使用python 3完成菜鸟。我有一些代码,无法弄清楚为什么我会继续得到我的输出。出于某种原因,elif语句未被识别。这是输出优先和下面的代码:
3
Your fortune for today is:
Please press enter to end
#Program for fortune cookies
var1 = "It's going to be a good day"
var2 = "You'll have a long life"
var3 = "Your life will be short"
var4 = "Things will be good"
var5 = "Life will be fun"
import random
randNum = random.randint(1, 5)
statement = ""
print(randNum)
if randNum == 1:
statement = var1
elif randNum == 2:
statement = var2
elif randNum == 3:
statement == var3
elif randNum == 4:
statement == var4
elif randNum == 5:
statement == var5
print("Your fortune for today is: ", statement)
input("Please press enter to end")
答案 0 :(得分:3)
您没有将varx
分配给statement
(在某些情况下,分别为3,4和5),但需要进行比较。只需改变所有:
statement == varx
要:
statement = var3
除此之外,似乎有效。
答案 1 :(得分:1)
检查行:
elif randNum == 3:
statement == var3
elif randNum == 4:
statement == var4
elif randNum == 5:
statement == var5
'=='用于检查相等性,'='用于分配变量。
答案 2 :(得分:0)
问题是例如
statement == var5

转让声明。
祝你好运!