【问题标题】:I would like to redirect my user to a specific question, from the first line of code, how do I do that? [duplicate]我想从第一行代码将我的用户重定向到一个特定的问题,我该怎么做? [复制]
【发布时间】:2019-02-05 10:14:48
【问题描述】:
print("Hello Welcome To The Essay Generating Code")
print("")
print("First things first, you need to give your essay characters")
print("")
print("")
print("How many main characters would you like your story to have?")
number_of_characters = input("Number Of Characters:")
if number_of_characters <= "10":
    print("That's way too many characters!") 
else:
    # rest of code

我想用else函数将用户重定向回输入字符数,我该怎么做?

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    我会根据您的目的使用函数并使用while 循环,直到您获得所需的值。

    def set_characters_number():
        print("Hello Welcome To The Essay Generating Code")
        print("")
        print("First things first, you need to give your essay characters")
        print("")
        print("")
        print("How many main characters would you like your story to have?")
        return (int(input("Number Of Characters:")))
    
    number_of_characters = set_characters_number()
    
    while (number_of_characters >= 10):
        print("That's way too many characters!")
        number_of_characters = set_characters_number()
    
    # rest of code
    

    或者,如果您不想再次显示欢迎消息:

    print("Hello Welcome To The Essay Generating Code")
    print("")
    print("First things first, you need to give your essay characters")
    print("")
    print("")
    print("How many main characters would you like your story to have?")
    
    number_of_characters = int(input("Number Of Characters:"))
    
    while (number_of_characters >= 10):
        print("That's way too many characters!")
        number_of_characters = int(input("Number Of Characters:"))
    
    # rest of code
    

    顺便说一句,我想你写的字符太多了大于等于 10 (&gt;= 10),而不是小于等于 10 (&lt;= 10)

    注意 int(input(...)) 将字符串从输入转换为 int,以便进行数字比较

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 2020-05-24
    • 1970-01-01
    • 2018-01-02
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2019-10-19
    相关资源
    最近更新 更多