[英]Python3 While Loop jumping to else statement
我在编程的前几周。 我试图做一个功能,要求用户输入,检查该输入是否为字母,如果是字母,那么我想打破while循环。
如果我运行脚本并在输入中输入正确的类型,它将中断并正常工作。 如果我有意在输入字段中输入数字,则当我重新输入正确的类型时它不会中断。
我在做什么错的任何想法?
def wallType():
wall_type = input("What type of wall was the route on? ")
while wall_type:
#if there is text in the input,instead of an alphanumeric, then tell them they must put in a number.
if str.isalpha(wall_type):
return wall_type
else:
print("You entered a number, you must enter a word. Try again. ")
wallType()
将您的代码放入while True
循环中。 当收到的输入是字母时,请使用break
中断while
循环。 否则,再次循环以请求输入。
def wallType():
while True:
wall_type = input("What type of wall was the route on? ")
# string is alphabetic, break out of loop
if str.isalpha(wall_type):
break
else:
print("Please reenter an alphabetic word. Try again. ")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.