我正在尝试为一件快速的事情创建一个交互式故事代码,而我的布尔值似乎并没有在代码中从true变为false。 在这里:
age = input ("Type in your age")
print("You're bored during quarantine and decide to go visit a haunted house alone. You visit the house, open the door, and walk in.")
print("You are in front of a creepy door in a hallway.")
print("What do you want to do?")
action = input ("Type: in, left, or right. Then click OK or press enter")
survive = False
def survival():
if age%2 == 0:
survive = False
else:
survive = True
survival()
if action == "in":
print("You choose to go in.")
print("The room is pitch black")
if survive == False:
print("A monster pops out of the corner and eats you!")
else:
print("You turn the light on and realize that this is just an old bathroom. Gross.")
if action == "left":
print("You choose to turn left.")
print("A ghost appears at the end of the hall.")
if survive == False:
print("The ghost chases you down and haunts your soul forever!")
else:
print("Your eyes mistook you, it was a spiderweb.")
if action == "right":
print("You choose to turn right")
print("A greenish light is visible in the distance")
if survive == False:
print("The light gets brighter and brighter, and you realize it was the headlamp of a ghost coal miner!")
else:
print("You go to check out the light, turns out it's some old christmas lighting still plugged in.")
if survive == False:
print("May your soul rest eternally as you join the other ghosts at the haunted house.")
else:
print("You survived your stay at the haunted house and apparently it's just a house.")
您可以看到我正在尝试获取该人的年龄,如果该值均匀地除以2,则将生存时间设置为false。但是,无论您进入多大的年龄,无论是偶数还是奇数,都仍然可以生存= false。有什么想法可能有问题吗?
答案 0 :(得分:1)
函数中的变量是局部变量,age
在函数外部。
因此,您需要在函数中使用global
关键字为
def survival():
global age
global survive
if age%2 == 0:
survive = False
else:
survive = True
第二,您将年龄输入作为字符串,当您使用运算符时,这将为您提供Type error
。您需要将其转换为整数
age=int(age)
或age=int(input())
。
最后,我建议您像@ juanpa.arrivillaga所说的那样在函数中传递值:
def survival():
age=int(input())
survive=False
或
def survival(age,survive=False):
答案 1 :(得分:0)
更改这段代码:
survive = False # Useless statement -- you replace the value on the following main-program line
def survival():
if age%2 == 0:
survive = False # No need to set a variable; just return the Boolean value
else:
survive = True
survival() # Make this line save a return value
对此:
def survival():
return age%2 == 1
survive = survival()
此外,我认为您需要更多关于布尔的练习。 从不将变量与布尔常量进行比较:请改用逻辑运算符。你在哪里
if survive == False:
替换为
if not survive:
更好的是,逆转生存检查的逻辑:
def kill_me():
return age%2 == 0
sudden_death = kill_me()
...
if sudden_death:
print("Cthulhu rises through the floor; his presence destroys your very soul.")
答案 2 :(得分:0)
解决此问题的一种方法如下,希望对您有所帮助
age = int(input ("Type in your age\n"))
print("You're bored during quarantine and decide to go visit a haunted house alone. You
visit the house, open the door, and walk in.")
print("You are in front of a creepy door in a hallway.")
print("What do you want to do?")
action = input ("Type: in, left, or right. Then click OK or press enter\n")
def survival(age=age):
if age%2 == 0:
return False
else:
return True
if action == "in":
print("You choose to go in.")
if survival() == False:
print("A monster pops out of the corner and eats you!")
else:
print("You turn the light on and realize that this is just an old bathroom. Gross.")
if action == "left":
print("You choose to turn left.")
print("A ghost appears at the end of the hall.")
if survival() == False:
print("The ghost chases you down and haunts your soul forever!")
else:
print("Your eyes mistook you, it was a spiderweb.")
if action == "right":
print("You choose to turn right")
print("A greenish light is visible in the distance")
if survival() == False:
print("The light gets brighter and brighter, and you realize it was the headlamp of a ghost coal miner!")
else:
print("You go to check out the light, turns out it's some old christmas lighting still plugged in.")
if survival() == False:
print("May your soul rest eternally as you join the other ghosts at the haunted house.")
else:
print("You survived your stay at the haunted house and apparently it's just a house.")
答案 3 :(得分:0)
只需在代码中修改以下内容即可。
您输入年龄作为字符串。在Python中, input()返回字符串值。您应该将输入值转换为整数。为此,您可以使用 int()方法。
age = int(输入(“键入您的年龄”))
或
age = input ("Type in your age")
age = int(age)
def survival():
global survive
global age
if age%2 == 0:
survive = False
else:
survive = True