如何使Python程序基于响应重新定义变量?

2024-06-01 09:21:50 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我已经为一个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.")

Tags: orandoftheinantimeis
3条回答

如果要将单个值与多个值进行比较,则使用这种语法将失败:

if my_animal is "cat" or "horse":
    my_operation()

这有几个原因。 首先,运算符优先级将语句解释为:

if (my_animal is "cat") or "horse":
    my_operation()

…并且不管(my_animal is "cat")的真实性如何,"horse"本身(或任何字符串、标量、非空序列)将被计算为True

此语法失败的第二个原因是is运算符检查两个对象是否是内存中的同一对象,而不是等效值

要纠正这一点,你可以说:

if my_animal == "cat" or my_animal == "horse":
    my_operation()

如果你有很多动物要检查,这会变得很麻烦。 在这种情况下,我建议:

valid_animals = ["cat", "fish", "horse", "dog"]
if my_animal in valid_animals:
    my_operation()

在检查不同大小写的情况下,我建议将字符串转换为全大写或小写:

if character_name.lower() == 'alice':
    character_action()

最后,如果你说PlayerAttributes == "clumsy" and "fast"之类的话,那你就太离谱了

首先,==是比较,而不是赋值

因此,修复这个问题,打开一个python解释器,并自行计算该语句。例如:

>>> PlayerAttributes = "clumsy" and "fast"
>>> print(PlayerAttributes)

您将得到True,因为正如我们上面所说的,字符串是“truthy”,并且True and True的计算结果为True

如果要为单个变量分配多个值,则需要使用列表、元组或其他序列

PlayerAttributes = ["clumsy", "fast"] # that's a list

要指定值,需要使用单个等号“=”而不是双倍“=”。double是一个比较运算符

一些建议:

  • 为变量选择不同的命名约定(请参阅PEP8
  • 考虑为玩家和玩家属性使用类
  • 在条件中使用“else”,以防用户输入意外内容

好的,我意识到我的程序对于语言和我的编码知识来说都太复杂了,所以我把它简化了很多。代码如下:

import time
import winsound
gender = ["boy", "girl", "Boy", "Girl"]
##It's simpler now. No set names, rooms, or big choices
print("Are you a boy or girl?")
genderChoice = input("Insert here ->  ")

name = input("What is your name?")
if gender = "boy" or gender = "Boy":
    friend = input("Your roommate walks in looking worried. Her name is...")

elif gender = "girl" or gender = "Girl"
    ##almost the same thing, just with the genders switched

相关问题 更多 >