这个问题在这里已有答案:
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函数重定向用户输入字符数,我该怎么做?
我将使用函数为您的目的,并使用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(>= 10
),而不是低于或等于10(<= 10
),就像你写的那样
注意int(input(...))
将字符串从输入转换为int,以使数值比较成为可能