首页 > 解决方案 > While 循环的第二次迭代会产生错误

问题描述

我对 Python 很陌生,我搜索过但找不到答案。先感谢您。

  1. 我故意输入了错误的答案,它会正确响应 - 提示我再次输入。

  2. 我现在输入正确的答案,它以错误响应(我的代码低于错误)

    ---------------------------------------------------------------------------
    

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

标签: python

解决方案


将您的代码更改为:

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()。


推荐阅读