最后 else if 语句不打印
Last else if statement does not print
如果角色是“学生”,我试图让输出显示“学生没有获得钥匙”,如果角色是“管理员”或“教师”,则显示“管理员和教师获得钥匙”,以及如果角色是这些选项中的 none,则“您只能是管理员、教师或学生”。使用下面的代码,第一个 if 语句和 elif 语句在用户键入“student”时正确显示“Students do not get keys”,在用户键入“administrator”或“时正确显示“Administrators and teachers get keys”老师”。 但是如果用户写“janitor”,输出仍然会显示“Administrators and teachers get keys”。我已经尝试在末尾添加第二个 elif 语句而不是 and else:
然后在它下面 if...:
,但结果是一样的。我还尝试混合 if 语句的顺序,但输出 永远不会打印“您只能是管理员、教师或学生”。我能做什么?谢谢。
role = input("Your role: ")
if role == "student":
print("Students do not get keys!")
elif role == "administrator" or "teacher":
print("Administrators and teachers get keys!")
else:
if role != "administrator" or "teacher" or "student":
print("You can only be an administrator, teacher, or student!")
试试这个:
role = input("Your role: ")
print(role)
if role == "student":
print("Students do not get keys!")
elif role == "administrator" or role =="teacher":
print("Administrators and teachers get keys!")
elif role != "administrator" or role!= "teacher" or role!="student":
print("You can only be an administrator")
将所有值放在一个列表中效果会好一些
role = input("Your role: ")
print(role)
roles = ["student","teacher","administrator"]
if role == "student":
print("Students do not get keys!")
elif role == "administrator" or role =="teacher":
print("Administrators and teachers get keys!")
elif role not in roles:
print("You can only be an administrator")
此代码:
role == "administrator" or "teacher"
永远是真的。你基本上是在说角色是“管理员”还是“老师”是真的。任何非空字符串都将评估为 True。所以你可能想要这样的东西:
role in ["administrator", "teacher"]
问题出在elif语句始终为真,解决此问题
试试这个代码:
role = input("Your role:")
if role == 'student':
print("Students do not get keys!")
elif role in ['administrator', 'teacher']:
print("Administrators and teachers get keys!")
else:
print("You can only be an administrator, teacher, or student!")
如果角色是“学生”,我试图让输出显示“学生没有获得钥匙”,如果角色是“管理员”或“教师”,则显示“管理员和教师获得钥匙”,以及如果角色是这些选项中的 none,则“您只能是管理员、教师或学生”。使用下面的代码,第一个 if 语句和 elif 语句在用户键入“student”时正确显示“Students do not get keys”,在用户键入“administrator”或“时正确显示“Administrators and teachers get keys”老师”。 但是如果用户写“janitor”,输出仍然会显示“Administrators and teachers get keys”。我已经尝试在末尾添加第二个 elif 语句而不是 and else:
然后在它下面 if...:
,但结果是一样的。我还尝试混合 if 语句的顺序,但输出 永远不会打印“您只能是管理员、教师或学生”。我能做什么?谢谢。
role = input("Your role: ")
if role == "student":
print("Students do not get keys!")
elif role == "administrator" or "teacher":
print("Administrators and teachers get keys!")
else:
if role != "administrator" or "teacher" or "student":
print("You can only be an administrator, teacher, or student!")
试试这个:
role = input("Your role: ")
print(role)
if role == "student":
print("Students do not get keys!")
elif role == "administrator" or role =="teacher":
print("Administrators and teachers get keys!")
elif role != "administrator" or role!= "teacher" or role!="student":
print("You can only be an administrator")
将所有值放在一个列表中效果会好一些
role = input("Your role: ")
print(role)
roles = ["student","teacher","administrator"]
if role == "student":
print("Students do not get keys!")
elif role == "administrator" or role =="teacher":
print("Administrators and teachers get keys!")
elif role not in roles:
print("You can only be an administrator")
此代码:
role == "administrator" or "teacher"
永远是真的。你基本上是在说角色是“管理员”还是“老师”是真的。任何非空字符串都将评估为 True。所以你可能想要这样的东西:
role in ["administrator", "teacher"]
问题出在elif语句始终为真,解决此问题 试试这个代码:
role = input("Your role:")
if role == 'student':
print("Students do not get keys!")
elif role in ['administrator', 'teacher']:
print("Administrators and teachers get keys!")
else:
print("You can only be an administrator, teacher, or student!")