所以我输入3作为我的眼睛颜色(绿色),它显示“您是一个乐观的人”,而不是“您是一个好奇的人”。为什么是这样?它不应该显示其他吗?
eyeList = ["blue", "brown","green","hazel","grey","none"]
print(eyeList)
eyecolor = int(input("Pick your eye color: "))
if eyecolor == 1 or 2:
print("you are a optimistic person")
else:
print("you are a curious person")
personality
可能是string
,请使用int(input(...))
获得int
int
与string
==
,不是=
:
或if
行后需要冒号elif
类似的事情可能起作用:
myList = ["shy","sociable","loud"]
print(myList)
try :
personality = int(input("Pick a trait from the list: "))
except ValueError :
sys.exit("Invalid input: " + str(personality))
if personality == 0 :
print("You are a person who doesn't doesn't like talking to other people")
elif personality == 1 :
print("you talk to people, but aren't really loud")
elif personality == 2 :
print("You love talking to people and you are very loud")
Python语法:
if condition:
Indented expressions
elif condition2:
Other expressions
else:
Further expressions
每个条件之后,您只是缺少colons(:
)。
此外,要检查某项是否等于其他项,必须使用==
。单个=
执行分配。
if variable == 42:
variable = 7
最后,您不能将整数与字符串进行比较(input ()
函数返回字符串)。为此,将字符串转换为整数:
IntegerValue = int(stringFormat)
最终提示:您的控制台为您提供了有关代码错误的有用提示。听他们说。
有3个问题:
a)输入为字符串,您需要在int
中进行转换b)您输入的语法错误,将是两个等号(==)
c)跳过if
结尾的:已解决的代码:
myList = ["shy","sociable","loud"]
print(myList)
personality = int(input("Pick a trait from the list: "))
if personality == 0:
print("You are a person who doesn't doesn't like talking to other people")
elif personality == 1:
print("you talk to people, but aren't really loud")
elif personality == 2:
print("You love talking to people and you are very loud")