Python中的空白输入

时间:2018-09-08 02:46:36

标签: python input while-loop

我正在学习while循环,并为该项目编写了此代码,该代码通过了除一个条件之外的所有资格。一种是当用户点击回车(不输入任何内容)时,它应该继续询问。我在程序中无法建立它。下面的代码:

def str_analysis(question):
    while True:   
        if question.isdigit() == True:
            if int(question) > 99:
                print (question,"is a big number!")
                break
            else:
                print (question,"is a small number!")
                break     
        if question.isalpha() == True:
            print (question,"is all alphabetical characters!")
            break
        else:
            print (question, "is neither all alpha nor all digit.")
            break


str_analysis(question = input("Enter word or integer: "))

我尝试将其放在

while True:
if question == "":
print ("") 

希望它会继续问 但是当我这样做时,它只会跳到(问题,“既不是全字母,也不是全数字。”)

2 个答案:

答案 0 :(得分:0)

可以稍微清理一下,但只解决了您的问题。

def str_analysis(question):
    while True:

        if question.isdigit() == True:

            if int(question) > 99:
                print (question,"is a big number!")
                break

            else:
                print (question,"is a small number!")
                break


        elif question.isalpha() == True:
            print (question,"is all alphabetical characters!")
            break

        else:

            print (question, "is neither all alpha nor all digit.")
            question = input("Enter word or integer: ")





str_analysis(question = input("Enter word or integer: "))

答案 1 :(得分:0)

可以使用while-loop来解决:

question = input("Enter word or integer: ")

def str_analysis(question):
    while True:   
        if question.isdigit() == True:
            if int(question) > 99:
                print (question,"is a big number!")
                break
            else:
                print (question,"is a small number!")
                break     
        if question.isalpha() == True:
            print (question,"is all alphabetical characters!")
            break
        else:
            print (question, "is neither all alpha nor all digit.")
            break


while len(question) == 0:
    question = input("Enter word or integer: ")

str_analysis(question)

工作原理:

每次用户按 Enter 时,它都会不断询问。