python - While 循环的第二次迭代会产生错误
问题描述
我对 Python 很陌生,我搜索过但找不到答案。先感谢您。
我故意输入了错误的答案,它会正确响应 - 提示我再次输入。
我现在输入正确的答案,它以错误响应(我的代码低于错误)
---------------------------------------------------------------------------
54 print("Enter Y/N:") 55 answer = input() ---> 56 answer = input.upper() 57 58 print("太好了!让我们开始吧。" )
AttributeError:'function'对象没有属性'upper'
`# intro text print("Are you ready to create a super hero with the super hero generator 3000?") # Ask for input / use input() to take it / use upper() to adjust text print("Enter Y/N:") answer = input() answer = answer.upper() # adding while loop to condition input(answer) while answer != "Y": print("Sorry, but you have to choose Y to continue.") print("Enter Y/N:") answer = input() answer = input.upper() print("Great! Lets get started.") ```
解决方案
将您的代码更改为:
while answer != "Y":
print("Sorry, but you have to choose Y to continue.")
print("Enter Y/N:")
answer = input()
answer = answer.upper()
input() 函数没有函数 upper(),您想在应答时调用 upper()。
推荐阅读
- gatsby - Gatsbyjs – 超过最大更新深度
- python - Pandas,跨行求和,取决于列内容
- xamarin - MvvmCross 6.2.1 不兼容的目标框架
- drop-down-menu - 如何防止带有 display:none 的表单字段被提交
- javascript - React 中的惰性是什么?
- laravel - Laravel 在创建事件时使用查询
- regex - 正则表达式匹配一个非常具体的模式
- javascript - 视频无法在移动设备上播放 > Wordpress Avada 主题融合滑块
- c# - 关闭时间后自动回复 C#
- css - 如何禁用主页上特色图像的裁剪