[英]Semantic Error with If ,elif, and else statements
您好,此消息可能會傳達給任何人。 我遇到了一個我無法弄清楚的語義錯誤問題。 我對編碼很陌生,我正在嘗試學習 python 3。我正在嘗試創建一個代碼,讓我知道我是否必須在一周中的特定日子工作。 我已經通過使用 if 語句來做到這一點,但由於某種原因,我無法將 go 的代碼傳遞給 else 或 elif。 任何和所有的答案都會有所幫助。 提前致謝。
week_day_1 = "Monday"
week_day_2 = "Tuesday"
week_day_3 = "Wednesday"
week_day_4 = "Thursday"
half_week_day = "Friday"
weekend_day_1 = "Saturday"
weekend_day_2 = "Sunday"
week_days = week_day_1, week_day_2, week_day_3, week_day_4
weekend_days = weekend_day_1, weekend_day_2
weekend_days = True
week_days = True
half_week_day = True
input("What day of the week is it?") #the input allows the user to insert the day of the week.
if week_days:
print("You must go to work.")
elif weekend_days:
print("It is the weekend. Enjoy your time off.")
elif half_week_day:
print("You must go to work, but your weekend start once you clock out.")
else:
print("Please enter day of the week.")**
無論哪一天我輸入代碼都會返回“您必須 go 才能工作”
您的代碼存在 3 個主要問題(還有更多):
1. week_days = week_day_1, week_day_2, week_day_3, week_day_4
應該是帶括號的列表。
2. 在向這些變量聲明列表后,您向這些變量聲明 boolean 值。
3. 沒有檢查輸入。 僅檢查始終為 True 的 boolean 值。
這是一個可以工作的代碼:
week_day_1 = "Monday"
week_day_2 = "Tuesday"
week_day_3 = "Wednesday"
week_day_4 = "Thursday"
half_week_day = "Friday"
weekend_day_1 = "Saturday"
weekend_day_2 = "Sunday"
week_days = [week_day_1, week_day_2, week_day_3, week_day_4]
weekend_days = [weekend_day_1, weekend_day_2]
day = input("What day of the week is it?") #the input allows the user to insert the day of the week.
if day in week_days:
print("You must go to work.")
elif day in weekend_days:
print("It is the weekend. Enjoy your time off.")
elif day == half_week_day:
print("You must go to work, but your weekend start once you clock out.")
else:
print("Please enter day of the week.")
您需要將用戶的輸入分配給一個變量,然后您可以將它與您創建的元組進行比較。
為了保持一致性,我將half_week_day
更改為half_week_days
,並將其設為一個元組(在作業中的week_day_5
之后放置一個,
)。
像week_days = True
這樣的分配正在替換列出每種日期類型的元組。 他們不需要。
week_day_1 = "Monday"
week_day_2 = "Tuesday"
week_day_3 = "Wednesday"
week_day_4 = "Thursday"
week_day_5 = "Friday"
weekend_day_1 = "Saturday"
weekend_day_2 = "Sunday"
week_days = week_day_1, week_day_2, week_day_3, week_day_4
weekend_days = weekend_day_1, weekend_day_2
half_week_days = week_day_5,
today = input("What day of the week is it?") #the input allows the user to insert the day of the week.
if today in week_days:
print("You must go to work.")
elif today in weekend_days:
print("It is the weekend. Enjoy your time off.")
elif today in half_week_day:
print("You must go to work, but your weekend start once you clock out.")
else:
print("Please enter day of the week.")
您現有代碼不起作用的主要原因是因為您沒有保存輸入值並進一步比較它,這就是它每次都執行最后一個else
的原因。 如果我是你,我會用這種方式來完成任務——
week_days = ["Monday", "Tuesday", "Wednesday", "Thursday"]
weekend_days = ["Saturday", "Sunday"]
half_week_day = "Friday"
day_name = input(
"What day of the week is it?"
) # the input allows the user to insert the day of the week.
if day_name in week_days:
print("You must go to work.")
elif day_name in weekend_days:
print("It is the weekend. Enjoy your time off.")
elif day_name == half_week_day:
print("You must go to work, but your weekend start once you clock out.")
else:
print("Please enter day of the week.")
解釋,
1.我使用list來存儲week_days
而不是多個變量。
2.我使用了一個新變量來存儲用戶輸入數據,以便我可以使用它與現有的week_days
、 weeked_days
和half_week_day
進行比較。
3.我用了which將幫助我們檢查一個值是否存在於多個值中。
if weekdays
基本上檢查 python 中 object 的“真實性”。 由於weekdays
是一個包含元素的數組,這將評估為 True (而空數組將評估為 False 例如if [] == False
)
你想要做的是將你的輸入分配給一個變量,比如day_to_check
,然后你想檢查這一天是否在這些 arrays 中。
例如:
day_to_check = "Monday"
if day_to_check in weekdays:
...
我建議為此編寫一個腳本並使用argparse
,在編寫腳本和 google argparse 時檢查如何使用 `if name == " main "。
此外,您可以通過使用 python 中的calendar
模塊使這更容易/更少樣板:
import calendar
print(calendar.day_name[0])
>>>Monday
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.