0

こんにちは私はPythonの初心者で、プログラムに日付を入力して表示するプログラムを作成しようとしていました

ユーザーが制限外の数値を入力したときのループを実装しようとしました。月を決定するために、while ループは正常に機能しました。

month = int(input("Which numeric month of the year were you born in?\n"))
while((month <=0) or (month >12)):
    print("The Month must be within the range 12>= Month >0. Please enter the value again.")
    print("\n")
    month = int(input("Which numeric month of the year were you born in?\n"))

ただし、2 番目の部分 (以下) では、ユーザーが 2 月 (28 日に制限されている) の値を入力する日を決定するため、表示されるループ メッセージは別の条件 (dayセット) 代わりに。

入力する場合: の場合は 2、 の場合はmonth30 ですday。ループするメッセージは次のとおりです。

...30< 月 =<0

表示する代わりに:

28< 月 =<0

while ステートメントの正しい使い方を教えてください。

私のコードは次のとおりです。

day = int(input("Which numeric day of the month were you born in?\n"))
while(month == 1,3,5,7,8,10,12):
    if(day <=0) or (day >31):
        print("For your selected month, the value for day must be within the range 31>= Day >0. Please enter the value again.")
        print("\n")
        day= int(input("Which numeric day of the month were you born in?\n"))
while(month ==2):
    if(day <=0) or (day >28):
        print("For your selected month, the value for day must be within the range 28>= Day >0. Please enter the value again.")
        print("\n")
        day= int(input("Which numeric day of the month were you born in?\n"))
while(month ==4,6,9,11):
    if(day <=0) or (day >30):
        print("For your selected month, the value for day must be within the range 30>=Day>0. Please enter the value again.")
        print("\n")
        day= int(input("Which numeric day of the month were you born in?\n"))

これを使用する場合、初心者レベルの Python コードのみに制限されていることに注意してください。これを超えてできることは、forループの代わりにwhileループを使用することですが、それ以上に高度なことはありません。

プログラムは、プログラムの最後に個人の生年月日を表示する必要があります。

4

1 に答える 1

3

while ループが多すぎます。それらのいくつかからは、決して逃げることはできません。

自分が何月なのかがわかったら、今月の日数を決定する必要があります。

だからあなたは持っています

month = int(input("Which numeric month of the year were you born in?\n"))
while((month <=0) or (month >12)):
    print("The Month must be within the range 12>= Month >0. Please enter the value again."):
    print("\n")
    month = int(input("Which numeric month of the year were you born in?\n"))
if month in (4, 6, 9, 11): # short
    maxdays = 30
elif month != 2: # not February -> long
    maxdays = 31
else:
    # Here we could ask for the year, determine if the year is divisible by 4, by 100 and by 400 and with this information determie if we have a leap year, but...
    # we are tolerant for now and accept the 29 as well.
    maxdays = 29

これで、あまり頻繁に繰り返すことなく、持っているもので作業できます。

day = int(input("Which numeric day of the month were you born in?\n"))
if(day <= 0) or (day > maxdays):
    print("For your selected month, the value for day must be within the range {0} >= Day > 0. Please enter the value again.".format(maxdays))
    print("\n")
    day = int(input("Which numeric day of the month were you born in?\n"))

まだ学んでいない場合はformat()、できる

    print("For your selected month, the value for day must be within the range " + str(maxdays) + " >= Day > 0. Please enter the value again.")

あるいは

    print("For your selected month, the value for day is wrong. Please enter the value again.")
于 2012-10-03T06:45:03.207 に答える