繁体   English   中英

If、elif 和 else 语句的语义错误

[英]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_daysweeked_dayshalf_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM