Python3在跳转到其他州的时候

2024-09-27 23:24:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始编程的几周。我试图做一个函数,要求用户输入,检查输入是否是字母,如果是字母,那么我想打破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()

Tags: ofthe函数in类型numberinputif
1条回答
网友
1楼 · 发布于 2024-09-27 23:24:58

将代码放入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.  ")

相关问题 更多 >

    热门问题