[英]Python if elif and else statement not working
I'm trying to write a short-ish script to entertain myself and potentially others.我正在尝试编写一个简短的脚本来娱乐自己和潜在的其他人。 When I run it it tends to just skip over the if elif and else statements.
当我运行它时,它往往会跳过 if elif 和 else 语句。
import random
adventure = ["fantasy" , "sci-fi" , "pirate" , "pre-history"]
setting = ["ocean", "forest" , "desert" , "castle"]
while True:
adven = []
setti = []
random.shuffle(adventure)
random.shuffle(setting)
adven.append(adventure[0])
setti.append(setting[0])
print(adven)
print(setti)
accept = input("Is this acceptable? ")
if accept == "y" :
print("Great, let us get started!")
break
else :
print("I am so sorry, lets try again!")
adve = []
sett = []
adve.append(adven)
sett.append(setti)
if adve == "fantasy" :
if sett == "ocean" :
print("1")
elif sett == "forest" :
print("2")
elif sett == "desert" :
print("3")
elif sett == "castle" :
print("4")
if adve == "sci-fi" :
if sett == "ocean" :
print("5")
elif sett == "forest" :
print("6")
elif sett == "desert" :
print("7")
elif sett == "castle" :
print("8")
if adve == "pirate" :
if sett == "ocean" :
print("9")
elif sett == "forest" :
print("10")
elif sett == "desert" :
print("11")
elif sett == "castle" :
print("12")
if adve == "pre-history" :
if sett == "ocean" :
print("13")
elif sett == "forest" :
print("14")
elif sett == "desert" :
print("15")
elif sett == "castle" :
print("16")
print(adve)
print(sett)
Would I need to keep it in the while True loop?我需要将它保存在 while True 循环中吗? I am not sure what to do because I want to make sure it works before I get any real details written into the script.
我不确定该怎么做,因为我想在我将任何真实的细节写入脚本之前确保它能够正常工作。
Try in
.试试看
in
As in:如:
if "fantasy" in advve:
Will likely work better for you than ==
here.可能比
==
这里更适合你。
Why?为什么? Because you're testing if a string is in a list of strings.
因为您正在测试字符串是否在字符串列表中。
Don't forget to swap your operands around.不要忘记交换你的操作数。 Unlike
==
in
cares which comes first.不像
==
in
cares 是第一位的。
As I understand, you would like to have multiple values in adven
and setti
, that's why it is a list?据我了解,您希望在
adven
和setti
中有多个值,这就是为什么它是一个列表?
So, firstly, you should take the definition of these values outside of loop scope:因此,首先,您应该在循环 scope 之外定义这些值:
adven = []
setti = []
while True:
...
You are appending list to list, so you getting list in list.您将列表附加到列表中,因此您在列表中获得列表。
adve = []
adve.append(["random", "stuff"])
print(adve)
# [['random', 'stuff']]
Comparing a list to a string is always false because you compare different types of values.将列表与字符串进行比较总是错误的,因为您比较的是不同类型的值。
print(adve == "random")
# False
print(["random"] == "random")
# False
To fix it you should:要修复它,您应该:
# adve.append(adven)
adve = adven.copy() # or simply `adve = adven`, or just use adven instead adve.
print(adve)
# ['random', 'stuff']
And:和:
# adve == "random"
print("random" in adve)
# True
Btw, you should use pop() function so you wouldn't get duplicates:顺便说一句,你应该使用pop() function 所以你不会得到重复:
# adven.append(adventure[0])
# setti.append(setting[0])
adven.append(adventure.pop())
setti.append(setting.pop())
If I misunderstood, and you don't need to handle multiple values for adven
and setti
, then your code should looks like this:如果我误解了,并且您不需要处理
adven
和setti
的多个值,那么您的代码应该如下所示:
import random
adventure = ["fantasy", "sci-fi", "pirate", "pre-history"]
setting = ["ocean", "forest", "desert", "castle"]
while True:
random.shuffle(adventure)
random.shuffle(setting)
adven = adventure[0]
setti = setting[0]
print(adven)
print(setti)
accept = input("Is this acceptable? ")
if accept == "y":
print("Great, let us get started!")
break
else:
print("I am so sorry, lets try again!")
adve = adven
sett = setti
multiplier = 0
if adve == "fantasy":
multiplier = 1
elif adve == "sci-fi":
multiplier = 2
elif adve == "pirate":
multiplier = 3
elif adve == "pre-history":
multiplier = 4
else:
print(f"issue with adve: {adve}")
if sett == "ocean":
print(multiplier * 1)
elif sett == "forest":
print(multiplier * 2)
elif sett == "desert":
print(multiplier * 3)
elif sett == "castle":
print(multiplier * 4)
print(adve)
print(sett)
and there is another thing that's wrong is that you just didn't put else statements after elif statements if you want to improve your coding basics.还有一件事是错误的,如果你想提高你的编码基础,你只是没有把 else 语句放在 elif 语句之后。 I recommend you to do your projects by watching projects like yours.
我建议您通过观看像您这样的项目来完成您的项目。
For example, you need to add this "else" statements like this:例如,您需要像这样添加这个“else”语句:
if adve == "fantasy" :
if sett == "ocean" :
print("1")
elif sett == "forest" :
print("2")
elif sett == "desert" :
print("3")
elif sett == "castle" :
print("4")
else :
print("I am so sorry, you picked a wrong choice")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.