こんにちは私は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、 の場合はmonth
30 です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
ループを使用することですが、それ以上に高度なことはありません。
プログラムは、プログラムの最後に個人の生年月日を表示する必要があります。