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

Python 函数不将布尔值从true更改为false,python,boolean,Python,Boolean,我正在尝试创建一个交互式故事代码,代码中的布尔值似乎没有从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

我正在尝试创建一个交互式故事代码,代码中的布尔值似乎没有从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。然而,无论你的年龄是多少,如果是偶数还是奇数,你仍然会得到survive=false。有什么问题吗?

函数中的变量是局部变量,
age
在函数之外。 因此,您需要在函数中使用
global
关键字作为


def survival():
全球时代
全球生存
如果年龄%2==0:
生存=错误
其他:
生存=真实
其次,您将年龄输入作为字符串,当您使用运算符时,这将导致
类型错误。您需要将其键入整数
age=int(age)
age=int(input())

最后,我建议您在函数中传递值,如@juanpa.arrivillaga所说:


def survival():
年龄=整数(输入()
生存=错误


def生存期(年龄,生存期=假):

更改此代码块:

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

解决这个问题的方法如下,希望能有所帮助

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

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

  • 您可以输入年龄作为字符串。在Python中,input()返回字符串值。您应该将输入值转换为整数。为此,可以使用int()方法

    年龄=整数(输入(“输入您的年龄”))

  • 或者

  • 在生存函数中使用年龄和生存。但两者都是在生存函数之外定义的。因此,在生存函数中使用global关键字。所以,代码应该被更正如下:

  • 因为函数merel创建了一个局部变量。您需要使用
    global my_variable
    ,以使助理在全局范围内执行操作。但你不应该。相反,将函数所需的值作为参数传递给函数,并从函数外部返回所需的值。不要使用可变的、全局的state@juanpa.arrivillaga对不起,我是python新手,我不太懂。所以我需要在函数中声明生存布尔值?
    age = input ("Type in your age")
    age = int(age)
    
    def survival():
      global survive
      global age
      if age%2 == 0:
        survive = False
      else:
        survive = True