我正在撰寫一個代碼,允許用戶輸入月份和日期,程式將告訴他們將在哪個季節獲得用戶輸入的資訊。我目前的作業是,當您輸入某些全天都有季節的月份時,它將顯示季節。但是當我輸入一個有 2 個季節的月份和日期時,它會給我一個錯誤。這是我正在關注的季節指南:
Spring: March 20 - June 20
Summer: June 21 - September 21
Autumn: September 22 - December 20
Winter: December 21 - March 19
我想弄清楚的是為什么在輸入某些日期時程式沒有列印出東西。例如,如果我輸入 3 月 25 日,它應該列印出 spring 但它沒有。
這是我的代碼:
input_month = input('Enter a month: ')
input_day = input('Enter a date: ')
month_lower = input_month.lower()
seasons = ()
if month_lower == 'january' or month_lower == 'febuary' or month_lower == 'march' and input_day <19:
seasons = 'winter'
elif month_lower == 'march' and input_day <=19 or month_lower == 'april' or month_lower == 'may' or month_lower == 'june' and input_day < 21:
seasons = 'spring'
elif month_lower == 'july' or month_lower == 'august' or month_lower == 'september' and input_day <22:
seasons = 'summer'
elif month_lower == 'october' or month_lower == 'november' or month_lower == 'december' and input_day <20:
seasons = 'autumn'
else:
print('Please enter a valid date')
print(seasons)
uj5u.com熱心網友回復:
你的春天條件有問題,你應該檢查一天是否嚴格優于19:
elif month_lower == 'march' and input_day > 19 or month_lower == 'april' or month_lower == 'may' or month_lower == 'june' and input_day < 21:
seasons = 'spring'
uj5u.com熱心網友回復:
我想你在輸入日期時得到輸入而不是 int 錯誤,因為你將 input_day 與 int 進行比較,快速解決此問題的方法是將 all 更改input_day為int(input_day). 但是,如果您的問題是 3 月 25 日沒有列印出您想要的內容而是給出了請輸入有效日期,那是因為您還沒有給它一個條件,以上所有條件均低于 20 但沒有人說話約 22-30
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/517213.html
