简体   繁体   English

Python 如何在其中添加一个循环,以便在用户将该字段留空时不断询问他们的姓名。 然后一旦输入名称,它就会返回

[英]Python How do I add a loop to this so that it keeps asking user for their name if they leave the field blank. Then once name is entered it returns

I want the system to check to make sure a name is entered.我希望系统检查以确保输入了名称。 If it is it will return Hello name I hope you enjoy this quiz.如果是,它将返回 Hello name 我希望你喜欢这个测验。 If not it gives an error message to say "this field cannot be left blank".如果不是,它会给出一条错误消息,提示“此字段不能留空”。

This gets the user to enter their name这让用户输入他们的名字

def hello():

    name=str(input("Please enter your name : "))
    
    if name == (""):
        print ("please enter your name this field cannot be blank")

    while name == (""):
        return
        name=str(input("Please enter your name : "))

    
    print("hello " + str(name))
    print ("I hope you enjoy this quiz")
    
hello() 

You are calling return before you check the name, so the function will return (exit) before anything happens.您在检查名称之前调用 return,因此 function 将在任何事情发生之前return (退出)。

Code:代码:

def hello():

    name = input("Please enter your name : ")
    while name == (""):
        print("please enter your name this field cannot be blank")
        name = input("Please enter your name : ")

    print("hello", name)
    print("I hope you enjoy this quiz")
   
hello()

Output: Output:

Please enter your name : 

please enter your name this field cannot be blank
Please enter your name : 
Ryan
hello Ryan
I hope you enjoy this quiz

Also note that the default type for input is str , so you do not need to cast it.另请注意, input的默认类型是str ,因此您不需要强制转换它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python-如何使用用户输入的名称创建文件夹? - Python - How to create a folder with a user entered name? Discord Python:如何保护变量中的用户名/ID,以便我可以使用它来调整他们的角色 - Discord Python: How do i safe a User Name/ID in a variable so i can use it to adjust their roles python中是否有任何方法可以通过在循环结束时要求用户重复一段时间或for循环来进行? - Is there any way in python to repeat a while or for loop by asking the user to do so at the end of the loop? Pydantic - 如何添加一个不断更改其名称的字段? - Pydantic - How to add a field that keeps changing its name? 如果函数返回用户输入的值,如何在不要求用户输入另一个值的情况下在另一个函数中使用该值 - If a function returns a value entered by a user, how can I use that value in another function without asking the user to input another value 如何查找在此字符串中输入的名称的缩写? - How do I find the initials of the name entered in this string? Python:如何在 for 循环中将 dataframe 的名称传递给 function? - Python: How do I pass name of a dataframe to a function in a for loop? Python-如何使用用户输入作为数组名称? - Python - How do I use user input as an array name? 使用python jira模块在jira中对用户进行身份验证时,如何继续询问用户名和密码直到正确? - How to keep asking for user name and password until correct while authenticating the user in jira using python jira module? 我如何制作用户配置文件,以便我输入姓名、邮件等,然后保存,以便我可以登录 - How do i make a user profile so that i will input the name, mail etc. and then it saves, so i can login
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM