仅输入字符串 [英] input string only

查看:39
本文介绍了仅输入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何要求用户只输入字符串.如果没有这样的东西,我如何设置它,以便如果他们输入任何其他内容,它会要求用户重新输入.

How do I ask the user to input the string only. If there isn't such a thing how do I set it so that if they enter anything else it would ask the user to re-enter.

这是我尝试过的(不起作用):

This is what I have tried (it doesn't work):

name = raw_input("Enter name: ")
 if name != str(name):
     name = raw_input("Error, please re-enter : ")

推荐答案

while True:
    userInput = raw_input("Enter a string only")
    if type(userInput) not str:
        continue
    else:
        print "You entered a STR data type. Congratulations"
        break

这篇关于仅输入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆