[英]Issue with very simple python3 program
Complete noob with python 3. I have some code and can't figure out for the life of me why I keep getting the output I do. 使用python 3来完成noob。我有一些代码,无法弄清楚我为什么会一直得到我做的输出。 For some reason the elif statements aren't getting recognized.
由于某些原因,elif语句无法识别。 Here is the output first and the code down below:
这是首先输出,下面是代码:
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")
You are not assigning varx
to statement
(in some cases, 3, 4 and 5th cases), but comparing. 您没有将
varx
分配给statement
(在某些情况下是3、4和5th),而是进行了比较。 Just change all: 只需更改所有内容:
statement == varx
To: 至:
statement = var3
Except from that, it seems to work. 除此之外,它似乎可行。
Check the lines: 检查行:
elif randNum == 3:
statement == var3
elif randNum == 4:
statement == var4
elif randNum == 5:
statement == var5
'==' is for checking equality, '=' is for assigning variables. '=='用于检查相等性,'='用于分配变量。
The Problem is for example 问题是例如
statement == var5
assignment statement. 作业说明。
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.