要求输入。如果输入不是str,请再次询问

时间:2020-06-06 17:41:56

标签: python input

总而言之,我需要问一个问题:

如果用户不引入str,则需要反复询问该问题,直到用户输入str。

这是我到目前为止所得到的。

x = input('What is your name? ')

if x != str
    x=input('Sorry, you need to introduce a str. What is your name? ')

1 个答案:

答案 0 :(得分:1)

我相信您的意思是,如果用户未输入所有字母,该程序将继续询问用户名称。您可以为此使用方法isalpha()

x = input('What is your name? ')

while not [s.isalpha() for s in x.split()] == [True for s in x.split()]:
    x = input('Sorry, you need to introduce a str. What is your name? ')