所以,我已经为一个Choose Your Own Adventure游戏编写了代码,或者至少是一个冒险游戏的开始,我需要知道如何让我的代码根据input()
响应更改变量的定义。这是我的密码:
import time
import winsound
PlayerAttributes = None
dream_item = None
print("ADVENTURE")
time.sleep(1)
print("Programmed and Developed by:")
print("Gabe 'GabeCodes' Chavez")
time.sleep(2)
print("We meet our heroes.")
print("Alice: The trained explorer doesn't give up hope.")
print("Although she isn't the strongest, she is the smartest person in the crew.")
print("")
time.sleep(6)
print("Sean: The fun loveable Irishman. He is quick on his feet, but not so much")
print("in the classroom. He has an innate sense of direction.")
print("")
time.sleep(6)
print("Mark: While he is strong, he is very ill-tempered and easy to aggravate.")
print("He tends to sleepwalk and has an inexplicable fear of elephants.")
print("")
time.sleep(6)
print("Amy: Amy is one of the kindest. Not just in the crew, but as an overall person.")
print("She is great with medicine and doesn't fear Mark's anger. They actually make a great balance,
and a cute couple.")
print("")
time.sleep(6)
print("Choose your Character... ")
PlayerChoice = input()
if PlayerChoice == "Alice" or "alice":
PlayerAttributes == "smart" and "fast"
dream_item == "golden totem"
elif PlayerChoice == "Sean" or "sean":
PlayerAttributes == "clumsy" and "fast"
dream_item == "little green eyeball"
elif PlayerChoice == "Mark" or "mark":
PlayerAttributes == "strong" and "smart"
dream_item == "tiny box"
elif PlayerChoice == "Amy" or "amy":
PlayerAttributes == "kind" and "healer"
dream_item == "dark iced coffee"
print("You wake with a start. You see your door, window, and dresser.")
time.sleep(2)
print("'What was I dreaming?' you thought. 'There was a", dream_item, "or something.")
如果要将单个值与多个值进行比较,则使用这种语法将失败:
这有几个原因。 首先,运算符优先级将语句解释为:
…并且不管
(my_animal is "cat")
的真实性如何,"horse"
本身(或任何字符串、标量、非空序列)将被计算为True
此语法失败的第二个原因是
is
运算符检查两个对象是否是内存中的同一对象,而不是等效值要纠正这一点,你可以说:
如果你有很多动物要检查,这会变得很麻烦。 在这种情况下,我建议:
在检查不同大小写的情况下,我建议将字符串转换为全大写或小写:
最后,如果你说
PlayerAttributes == "clumsy" and "fast"
之类的话,那你就太离谱了首先,
==
是比较,而不是赋值因此,修复这个问题,打开一个python解释器,并自行计算该语句。例如:
您将得到
True
,因为正如我们上面所说的,字符串是“truthy”,并且True and True
的计算结果为True
如果要为单个变量分配多个值,则需要使用列表、元组或其他序列
要指定值,需要使用单个等号“=”而不是双倍“=”。double是一个比较运算符
一些建议:
好的,我意识到我的程序对于语言和我的编码知识来说都太复杂了,所以我把它简化了很多。代码如下:
相关问题 更多 >
编程相关推荐