python - 正确输入后尝试除块不通过
问题描述
我是 Python 和一般编码的新手,并且在我的代码中遇到了一些错误。每当我在我的 Try/Except 代码块中输入错误的输入时,控制台就会打印“无效输入”但是,每当我在控制台中输入正确的短语时,它仍然会显示“无效输入”。我在网上查看并尝试使用这些代码行来解决这个问题(用## 表示),但我仍然遇到同样的问题。
例如,我会用正确的大小写和所有内容输入“Mad Libs”,但仍然会从我的 != 命令中得到“无效输入”。这可以通过以不同的方式格式化来轻松解决吗?这发生在所有 3 场比赛中。
如何解决这个问题?提前致谢!
def game_selection(): ##
pass ##
while True: ##
try:
playerChoice = input("So, which game would you like to play?: ")
if playerChoice != "Mad Libs":
print("Invalid input")
elif playerChoice != "Guessing Game":
print("Invalid input")
elif playerChoice != "Language Maker":
print("Invalid input")
continue ##
except:
print("Invalid Input")
game_selection() ##
print("Got it! " + playerChoice + " it is!")
sleep(2)
if playerChoice == "Mad Libs":
print("Initializing 'Mad Libs'.")
sleep(.5)
print("Welcome to MadLibs, " + playerName + "! There are a few simple rules to the game.")
print("All you have to do is enter in a phrase or word that is requested of you.")
playerReady = input("Ready to begin? Y/N")
解决方案
因为您要求它在此代码中回答无效输入
While True: ##
try:
playerChoice = input("So, which game would you like to play?: ")
if playerChoice != "Mad Libs":
print("Invalid input")
elif playerChoice != "Guessing Game":
print("Invalid input")
elif playerChoice != "Language Maker":
print("Invalid input")
continue ##
except:
print("Invalid Input")
推荐阅读
- c++ - SDL2 - 如何从外部像素阵列正确流式传输纹理(使用锁定和解锁)
- access-token - Insomnia 为其他 API 调用配置 OAuth 2.0 grant_type 密码访问令牌
- python - 如何在库存透视报告视图中添加新字段。奥多 14
- javascript - 从 API Json 获取价值
- javascript - 无法使用 NuxtJS + Vite 加载 SVG
- javascript - WinstonJS 在文件过大时使用文件名中的时间戳或索引旋转文件
- spring - Spring Data CrudRepository 的保存抛出 InvocationTargetException
- python - 正则表达式:模式无法匹配我正在寻找的内容
- javascript - apolloLink 中的 useReactVar 导致在渲染差异组件时无法更新组件
- css - 在 Elementor 部分中适合背景图像