【问题标题】:Function not changing boolean from true to false函数不会将布尔值从 true 更改为 false
【发布时间】:2020-07-31 05:39:57
【问题描述】:

我正在尝试为快速的事情创建一个交互式故事代码,而我的布尔值似乎没有在我的代码中从 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。有什么可能出错的想法吗?

【问题讨论】:

  • 因为您的函数 merel 创建了一个局部变量。您需要使用 global my_variable 来进行全局操作。但你不应该。相反,将函数需要的值作为参数传递给函数,并从函数外部返回所需的值。不要使用可变的全局状态
  • @juanpa.arrivillaga 对不起,我是 python 新手,我不太明白。所以我需要在函数中声明生存布尔值?

标签: python boolean


【解决方案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):

【讨论】:

    【解决方案2】:

    更改这部分代码:

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

    【讨论】:

      【解决方案3】:

      解决方法如下,希望对你有帮助

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

      【讨论】:

        【解决方案4】:

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

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

          age = int(input ("输入你的年龄"))

        或者,

        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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-07
          • 1970-01-01
          • 2019-09-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多