【问题标题】:User input-based if statement gives unexpected output基于用户输入的 if 语句给出了意外的输出
【发布时间】:2019-09-21 08:21:18
【问题描述】:

我正在制作一个基于文本的小游戏来练习我的 Python 技能。我正在努力让 if 语句根据我的用户输入显示正确的结果。

weapon_choice = str(input("You can choose between three weapons to defeat the beast!"
                   " Press 1 for Axe, 2 for Crossbow, 3 for Sword."))

if input(1):
    print("You chose the Axe of Might")
elif input(2):
    print("You chose the Sacred Crossbow")
else:
    print("You chose the Elven Sword")

我希望输出会询问我一个数字(1、2 或 3),然后打印与该数字关联的字符串。相反,当我输入 1 时,它会打印 1,然后是 2,然后是与数字 3 关联的字符串('else' 选项),无论我输入什么数字。我不明白为什么?

Greetings, weary wanderer.
Welcome to Freyjaberg. Choose your weapon.
You can choose between three weapons to defeat the beast! Press 1 for Axe, 2 for Crossbow, 3 for Sword.1
1
2
You chose the Elven Sword

Process finished with exit code 0

【问题讨论】:

  • 除了用1 提示用户之外,您还期望if input(1) 做什么?

标签: python if-statement input


【解决方案1】:

试试这个:

weapon_choice = input("You can choose between three weapons to defeat the beast!\nPress 1 for Axe, 2 for Crossbow, 3 for Sword.\n")

if weapon_choice=='1':
    print("You chose the Axe of Might")
elif weapon_choice=='2':
    print("You chose the Sacred Crossbow")
else:
    print("You chose the Elven Sword")

注意:

  1. weapon_choice 不需要转换为 str 格式,因为 input() 已经是字符串格式。
  2. 每当您执行input(1)input(2) 时,它基本上都会提示用户提供另一个输入,而不是检查条件。

输出:

michael@arkistarvh:/$ python text_game.py 
You can choose between three weapons to defeat the beast! Press 1 for Axe, 2 for Crossbow, 3 for Sword.
3
You chose the Elven Sword

【讨论】:

  • 我明白了!非常感谢您解释这一点!如果有意义,我试图以与列表相同的方式引用“输入”(考虑用户是否输入 1,然后显示此内容)。我现在明白我做错了什么。
【解决方案2】:

你应该有:

weapon_choice = input("You can choose between three weapons to defeat the beast! \n " +  " Press 1 for Axe, 2 for Crossbow, 3 for Sword.")

if weapon_choice == '1':
    print("You chose the Axe of Might")
elif weapon_choice == '2':
    print("You chose the Sacred Crossbow")
elif weapon_choice == '3':
    print("You chose the Elven Sword")
else:
    print("Invalid input selected")

这样做的原因是input(..) 导致解析字符串,因此input(..) 周围不需要str(..)。此外,您应该有一个传递无效输入的条件,以便更清楚地通知用户错误的根本原因。

(注意\n 表示新行的开始)

【讨论】:

  • 谢谢!我将为无效输入添加第四个选项,这是个好主意!
  • 嗨@Stoner!我添加了“无效输入”选项,但我希望它返回循环。我试过while True: weapon_choice <= '4' if weapon_choice=='1': print("You chose the Axe of Might") break elif weapon_choice=='2': print("You chose the Sacred Crossbow") break elif weapon_choice=='3': print("You chose the Elven Sword") break else: weapon_choice >= '4' print("Wanderer, there is no such weapon. Please choose from the ones available.") continue 但它不起作用:(
  • 我收到Traceback (most recent call last): File "/Users/elissavetvelli/PycharmProjects/Giraffe/2dlist.py", line 10, in <module> weapon_choice <= '4' NameError: name 'weapon_choice' is not defined。我已经尝试了很多东西,充其量我得到它是为了显示```流浪者,没有这样的武器。请从可用的选项中无限选择!
  • @nephele 你应该在循环之外有'weapon_choice'。如果您将它放在循环中,它将在每次迭代期间“刷新”,因此是无限循环。无论如何,社区中的惯例是每个问题有 1 个问题,因此最好将与原始问题无关的其他问题作为一个新的单独问题发布。这里的人可能不会再查看已关闭的问题,因此您的新问题可能会被错过。 :')
  • 谢谢!从现在开始我会这样做 - 感谢您花时间解释。
【解决方案3】:

这不是 Python 中输入的工作方式。

您正确地假设 input("some text") 会在第一行打印该文本(并将结果存储在变量 Weapon_choice 中),那么您为什么认为 input(number) 会返回一个布尔值来说明如果输入的是那个数字,你呢?

相反,它的作用是再次打印数字并返回一个空字符串(因为您可能只是按下了 Enter 键),所以前两个 if 为 False,程序进入 else,打印“Invalid input selected”。

您第一次输入的结果将存储在武器选择中,因此您应该对该变量进行比较。

【讨论】:

    【解决方案4】:

    if input(1) 使用参数1 调用input 函数,然后使用响应来测试是否执行其块中的代码。由于执行 input 只是将其参数打印到终端,并返回用户输入的任何响应,因此您的程序的行为与您的代码所暗示的完全一样。

    由于您已经将用户对第一个问题的回答存储在名为 weapon_choice 的变量中,因此您应该将 if input(1) 替换为 if weapon_choice == '1' - 其他的也一样。

    【讨论】:

      【解决方案5】:

      你可以试试这个脚本

      weapon_choice = str(input("You can choose between three weapons to defeat the beast!"
                         " Press 1 for Axe, 2 for Crossbow, 3 for Sword."))
      if weapon_choice == '1':
          print("You chose the Axe of Might")
      elif weapon_choice == '2':
          print("You chose the Sacred Crossbow")
      else:
          print("You chose the Elven Sword")
      

      【讨论】:

      • 不,你不能。 raw_input 在 Python 3 中甚至都不存在,Python 2 即将退役
      猜你喜欢
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 2012-11-07
      • 2021-07-26
      • 2018-08-01
      • 2016-03-11
      相关资源
      最近更新 更多