while True:
try:
horoscope= int(input("Enter what sign you are: "))
except ValueError:
print("Sorry, please try again")
continue
else:
break
if horoscope =='1':
print("You are a Gemini!")
elif horoscope =='2':
print("You are a Pisces!")
else:
print("Mediate more and try again.")
我正在尝试编写一个非常简单的代码,要求用户输入他们的星座,并根据他们的内容显示一条消息。到目前为止,我已将其设置为仅将用户输入与整数进行比较,但我希望用户能够输入其占星术符号的全名,而不是将符号分配给某些整数并通过这种方式过滤打印语句。我该怎么做呢?
答案 0 :(得分:0)
在Python中,您可以比较这样的字符串:
if horoscope=="Pisces":
#more code here
两个字符串必须完全相同,例如:Pisces
与pisces
不同。您可以阅读有关字符串here
答案 1 :(得分:0)
更改
horoscope= int(input("Enter what sign you are: "))
到
horoscope= input("Enter what sign you are: ")
然后在if-else
语句
while True:
try:
horoscope= input("Enter what sign you are: ")
except ValueError:
print("Sorry, please try again")
continue
else:
break
if horoscope.lower() == 'gemini':
print("You are a Gemini!")
elif horoscope.lower() == 'pisces':
print("You are a Pisces!")
else:
print("Mediate more and try again.")