2012-05-08 12 views
0

私は最近、ユーザーが月を入力し、プログラムに何日あるのかを表示するプログラムを作成する必要があるため、このサイトで登録しました。その月。また、ユーザーが2月の月を入力した場合のうるう年であるかどうかをプログラムが確認できるように、ユーザーは年を入力する必要があります。カレンダープログラムのヘルプ(文字列変数)

これは私がこれまでに作ってみた符号化である:

def get_year(): 
    year = raw_input("Please enter the year: ") 
    return year 

def get_month(): 
    month = raw_input("Please enter the month: ") 
    return month 

def leap_year(year): 
    if year % 4 == 0: 
     return true 
    else: 
     return false 

def get_days(month): 
    if month == "january": 
     print "31 days" 
    elif month == "february" and leap_year == true: 
     print "29 days" 
    else: 
     print "28 days" 
    if month == "march": 
     print "31 days" 
    elif month == "april": 
     print "30 days" 
    elif month == "may": 
     print "31 days" 
    elif month == "june": 
     print "30 days" 
    elif month == "july": 
     print "31 days" 
    elif month == "august": 
     print "31 days" 
    elif month == "september": 
     print "30 days" 
    elif month == "october": 
     print "31 days" 
    elif month == "november": 
     print "30 days" 
    elif month == "december": 
     print "31 days" 
    else: 
     print 

def main(): 
    user_year = get_year() 
    user_month = get_month() 
    leap_year(user_year) 
    get_days(user_month) 

main() 

とにかく、それは私のget_daysにエラーがあることは明らかだが、そのちょうど私がコードを書くことが かわからない機能しますユーザーが 月または3月のような月を入力したことをプログラムが知っていること。私は、入力が変数でなければならず、各 月が文字列であるため、可変文字列が必要であると推測しています。しかし、私はこれについて完全に間違っている可能性があります。

私は非常にPythonの新機能です(ちょうど今2週間、学校での作業のためにプログラミングをしています)ので、誰かが適切な方向に私を助けることができるならば、 、それは非常に高く評価されるだろう!

+1

を求めます。 html)をpython [標準ライブラリ](http://docs.python.org/library/index.html)から入手できますか? – srgerg

+0

あなたはかなり近いです。 2つのヒント: 'get_days'は2つの引数をとる必要があります。なぜなら、あなたは月を必要とするからです(うるう年をテストするために)。また、 'leap_year'は関数なので、それを呼び出して引数を渡す必要があります。また、あなたの 'if'ステートメントはちょっとばかりです。あなたが期待することを確実にするためにそれらを振り返ってみてください。そして最後に、それは '真'と '偽'ではなく、 '真'と '偽'でなければなりません。 – senderle

+0

また、うるう年を決定するアルゴリズムが非常に正確であるかどうかはわかりません。 Wikipeadiaから:「100で割り切れる年は、閏年でもなく、400で割り切れる場合を除き、うるう年」http://en.wikipedia.org/wiki/Leap_year – Levon

答えて

0

あなたはまあ近くでした。私は私がコメントしたいくつかの変更を加えました。 Raymond Hettingerが指摘するように、get_days(month)で、完全に壊れたです。

def get_year(): 
    year = raw_input("Please enter the year: ") 
    return int(year) #otherwise it is a string! 

def get_month(): 
    month = raw_input("Please enter the month: ") 
    return month 

def leap_year(year): 
    if year % 4 == 0: 
     return True 
    else: 
     return False 

def get_days(month,leap_year): #leap_year must be passes to this function 
    #This checks for "january" and "february" with leap years 
    #and falls back to last option on EVERYTHING ELSE like a feb without a leap year or even a "march" 
    if month == "january": 
     print "31 days" 
    elif month == "february" and leap_year == True: 
     print "29 days" 
    else: 
     print "28 days" 

    #this is a separate block that runs AFTER the previous block 
    if month == "march": 
     print "31 days" 
    elif month == "april": 
     print "30 days" 
    elif month == "may": 
     print "31 days" 
    elif month == "june": 
     print "30 days" 
    elif month == "july": 
     print "31 days" 
    elif month == "august": 
     print "31 days" 
    elif month == "september": 
     print "30 days" 
    elif month == "october": 
     print "31 days" 
    elif month == "november": 
     print "30 days" 
    elif month == "december": 
     print "31 days" 
    else: 
     print "invalid input" #so that it doesnt fail silently when I enter 2 

def main(): 
    user_year = get_year() 
    user_month = get_month() 
    leap_status = leap_year(user_year) #store the leap_year status to a variable 
    get_days(user_month, leap_status) #and then pass it to a function 

main() 
+0

は、leap_status変数やleap_yearを渡すことさえ考えなかった。ありがとうございます。私は仕事をするプログラムを持っています:) – Jertise

+0

答えの左側にあるチェックマークをクリックすることであなたを満足させる答えを**受け入れる**ことを忘れないでください。報酬は、このサイトをパワーアップします。誰もあなたに思い出させることはありません:-) – aitchnyu

0

if-elifが2月に壊れていました。マイナーチェンジは、if-elifロジックの中断のないパターンで続行することです:

def get_days(month): 
    if month == "january": 
     print "31 days" 
    elif month == "february" and leap_year: 
     print "29 days" 
    elif month == "february" and not leap_year: 
     print "28 days" 
    elif month == "march": 
     print "31 days" 
    elif month == "april": 
     print "30 days" 
    elif month == "may": 
     print "31 days" 
    elif month == "june": 
     print "30 days" 
    elif month == "july": 
     print "31 days" 
    elif month == "august": 
     print "31 days" 
    elif month == "september": 
     print "30 days" 
    elif month == "october": 
     print "31 days" 
    elif month == "november": 
     print "30 days" 
    elif month == "december": 
     print "31 days" 
    else: 
     print 'unknown month' 
+0

私はあなたのコメントを読んだので、はるかに理にかなっている、私は本当に私はそれのように考えることはできませんでしたが、 ! – Jertise

0

ビルドインデータ型のPythonを使用することをお勧めします。

def get_days(year, month): 
    """ 
    take year and month ,return the days in that month. 
    """ 
    #define a dictionary and the month is key which value is days 
    daydict = dict()  
    daydict = ['january'=31, 'february'=28, 'februaryinleapyear'=29, 
             'march'=31, 'april'=30, 
             'may'=31, 'june'=30, 
             'july'=31, 'august'=31, 
             'september'=30, 'october'=31, 
             'november'=30, 'december'=31 ] 

    try: 
     if month in daydict: 
      if month == 'february' and leap_year(year):  
       print daydict[februaryinleapyear] 
      else: 
       print daydict[month] 
     else: 
      print 'The year or month you input is invalid !' 
    except: 
     print 'error!' 
0
n=int(input('enter no of days in a month= ')) 
#0=sunday 
#1=moday 
#2=tuesday 
#3=wednesday 
#4=thursday 
#5=friday 
#6=saturday 
d=int(input('enter the starting day of month= ')) 
print('sun','mon','tue','wed','thu','fri','sat',sep='\t') 
for j in range(d): 
    print (' ',end='\t') 
i=1 
while(i<=n): 
    print (i,end='\t') 
    if(i+d)%7==0: 
     print('\t') 
    i=i+1 
+1

このコードのみの回答についての説明はおそらく有用でしょう。 – Pyves

+0

月の日数と月の開始日を受け入れてカレンダーを印刷します。それは毎年役に立ちます –

+0

私のコードが便利です –

0

このコードは、[カレンダーモジュール](http://docs.python.org/library/calendarあなたのpythonを使用して考えがあり、ユーザ入力(more simple codes

#python calendar days in month. 
month= input ("Enter the month('January', ...,'December'): ") # ask for inputs from user 

start=input ("Enter the start day ('Monday', ..., 'Sunday'): ") 


if start== "Monday" : 
    num=1 
elif start== "Tuesday" : 
    num=0 

elif start== "Wednesday" : 
    num=-1 

elif start== "Thursday" : 
    num=-2 

elif start== "Friday" : 
    num=-3 

elif start== "Sartday" : 
    num=-4 
elif start=="Sunday": 
    num=-5 


print(month)  
print("Mo Tu We Th Fr Sa Su") #the header of the Calender 


if month== "January" or month=="March" or month=="May" or month=="July" or month=="August" or month=="October" or month=="December" : 

    #for month with 31 days 
    for num_ in range (num,num+41,7): 
     for i in range(7): 
      if num<1: 
       print (' ',end="") 
      elif num>31: 
       print("",end="") 
      else: 
       print ("{0:>2}".format(num),end="") 
       if i<6 and num<31: 
        print(" ",end="") 
      num +=1 
     print() 


elif month== "April" or month=="June" or month=="Septemmber" or month=="November": 
    #for month with 30 days  
    for num_ in range (num,num+41,7): 
     for i in range(7): 
      if num<1: 
       print (' ',end="") 
      elif num>30: 
       print("",end="") 
      else: 
       print ("{0:>2}".format(num),end="") 
       if i<6 and num<30: 
        print(" ",end="") 
      num +=1 
     print() 


elif month== "February" : 
    # february is an exception : it has 28 days 
    for num_ in range (num,num+41,7): 
     for i in range(7): 
      if num<1: 
       print (' ',end="") 
      elif num>28: 
       print("",end="") 
      else: 

       print ("{0:>2}".format(num),end="") 
       if i<6 and num<28: 
        print(" ",end="") 
      num +=1 
     print() 


else: 
    print("invalid entry") 
関連する問題