函数未将布尔值从true更改为false



我正试图创建一个交互式故事代码,但我的布尔值在代码中似乎没有从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,那么将生存设置为假。然而,无论你的年龄是多少,你仍然可以存活下来=无论是偶数还是奇数,都是假的。有什么可能出错的想法吗?

函数中的变量是局部变量,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.arrivilaga所说的值:


def survival():
age=int(input())
survive=False


def survival(age,survive=False):

更改您的代码块:

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()

此外,我认为你需要更多地练习布尔型Never将变量与布尔常量进行比较:请改用逻辑运算符。在哪里

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.")

解决这个问题的一种方法如下,希望它能帮助

age = int(input ("Type in your agen"))
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 entern")
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.")

只需在代码中编辑以下内容。

  1. 您将年龄输入为字符串。在Python中,input((返回字符串值。您应该将输入值转换为整数。要做到这一点,您可以使用int((方法。

    age=int(输入("键入您的年龄"(

age = input ("Type in your age")
age = int(age)
  1. 您使用年龄并在生存功能中生存。但两者都是在生存功能之外定义的。因此使用global关键字在生存函数中使用它们。因此,应按以下方式更正《规范》:
def survival():
global survive
global age
if age%2 == 0:
survive = False
else:
survive = True

最新更新