【问题标题】:Can't get string user input working (Python)无法使字符串用户输入工作(Python)
【发布时间】:2022-11-03 14:42:02
【问题描述】:

我已经搜索了网络和这个网站,整天都在胡闹,尝试了 100 种方法来让这个简单的小程序正常工作。我正在练习无休止的 While 循环和字符串用户输入。谁能解释我做错了什么?谢谢!

while True:
    print("This is the start.")

    answer = input("Would you like to continue? (Y/N) ")
    answer = answer.islower()
    if answer == "n":
        print("Ok thank you and goodbye.")
        break
    elif answer == "y":
        print("Ok, let's start again.")
    else:
        print("You need to input a 'y' or an 'n'.")

【问题讨论】:

  • 错字。你想要answer = answer.lower()。 islower() 返回一个布尔值,表示变量是否已经是小写。

标签: python string input while-loop


【解决方案1】:

你的代码有一个问题

while True:
    print("This is the start.")

    answer = input("Would you like to continue? (Y/N) ")
    answer = answer.lower() # change from islower() to lower()
    if answer == "n":
        print("Ok thank you and goodbye.")
        break
    elif answer == "y":
        print("Ok, let's start again.")
    else:
        print("You need to input a 'y' or an 'n'.")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 2019-03-12
    • 2017-02-23
    • 2018-01-20
    • 2023-03-28
    • 1970-01-01
    • 2013-05-14
    相关资源
    最近更新 更多