用户输入的脚本
User inputted script
我正在尝试 运行 一个询问用户最喜欢的运动队的脚本。这是我目前所拥有的:
print("Who is your favorite sports team: Yankees, Knicks, or Jets?")
if input is "Yankees":
print("Good choice, go Yankees")
elif input is "Knicks":
print("Why...? They are terrible")
elif input is "Jets":
print("They are terrible too...")
else:
print("I have never heard of that team, try another team.")
每当我 运行 这个脚本时,最后一个 "else" 函数会在用户输入任何内容之前接管。
此外,none 个可供选择的团队已定义。有帮助吗?
输入是一个要求用户回答的函数。
您需要调用它并将 return 值分配给某个变量。
然后检查那个变量,而不是 input
本身。
备注
你可能想要 raw_input()
而不是得到你想要的字符串。
记得去除空格。
您的主要问题是您正在使用 is
来比较值。正如这里的问题所讨论的那样 --> String comparison in Python: is vs. ==
You use ==
when comparing values and is
when comparing identities.
您可能希望将代码更改为如下所示:
print("Who is your favorite sports team: Yankees, Knicks, or Jets?")
if input == "Yankees":
print("Good choice, go Yankees")
elif input == "Knicks":
print("Why...? They are terrible")
elif input == "Jets":
print("They are terrible too...")
else:
print("I have never heard of that team, try another team.")
但是,您可能需要考虑将您的代码放入 while
循环中,以便向用户询问问题,直到您的答案被接受为止。
您可能还需要考虑添加一些人为错误容忍度,方法是将比较值强制转换为小写字母。这样只要队名拼写正确,他们的对比就会准确无误。
例如,看下面的代码:
while True: #This means that the loop will continue until a "break"
answer = input("Who is your favorite sports team: Yankees, Knicks, or Jets? ").lower()
#the .lower() is where the input is made lowercase
if answer == "yankees":
print("Good choice, go Yankees")
break
elif answer == "knicks":
print("Why...? They are terrible")
break
elif answer == "jets":
print("They are terrible too...")
break
else:
print("I have never heard of that team, try another team.")
我正在尝试 运行 一个询问用户最喜欢的运动队的脚本。这是我目前所拥有的:
print("Who is your favorite sports team: Yankees, Knicks, or Jets?")
if input is "Yankees":
print("Good choice, go Yankees")
elif input is "Knicks":
print("Why...? They are terrible")
elif input is "Jets":
print("They are terrible too...")
else:
print("I have never heard of that team, try another team.")
每当我 运行 这个脚本时,最后一个 "else" 函数会在用户输入任何内容之前接管。
此外,none 个可供选择的团队已定义。有帮助吗?
输入是一个要求用户回答的函数。
您需要调用它并将 return 值分配给某个变量。
然后检查那个变量,而不是 input
本身。
备注
你可能想要 raw_input()
而不是得到你想要的字符串。
记得去除空格。
您的主要问题是您正在使用 is
来比较值。正如这里的问题所讨论的那样 --> String comparison in Python: is vs. ==
You use
==
when comparing values andis
when comparing identities.
您可能希望将代码更改为如下所示:
print("Who is your favorite sports team: Yankees, Knicks, or Jets?")
if input == "Yankees":
print("Good choice, go Yankees")
elif input == "Knicks":
print("Why...? They are terrible")
elif input == "Jets":
print("They are terrible too...")
else:
print("I have never heard of that team, try another team.")
但是,您可能需要考虑将您的代码放入 while
循环中,以便向用户询问问题,直到您的答案被接受为止。
您可能还需要考虑添加一些人为错误容忍度,方法是将比较值强制转换为小写字母。这样只要队名拼写正确,他们的对比就会准确无误。
例如,看下面的代码:
while True: #This means that the loop will continue until a "break"
answer = input("Who is your favorite sports team: Yankees, Knicks, or Jets? ").lower()
#the .lower() is where the input is made lowercase
if answer == "yankees":
print("Good choice, go Yankees")
break
elif answer == "knicks":
print("Why...? They are terrible")
break
elif answer == "jets":
print("They are terrible too...")
break
else:
print("I have never heard of that team, try another team.")