Python 不同输入失败后,输入提示未返回到上一个提示

Python 不同输入失败后,输入提示未返回到上一个提示,python,input,Python,Input,我的任务是检查是否输入了姓名和年龄,如果为空,则再次提示用户输入 当用户输入一个空输入时,它会进入年龄输入,然后它会正确提示,但是如果用户输入了非有效输入,然后再次输入空白输入,则不会再次给他们空白输入提示,只是非有效输入。我不知道如何解决这个问题 这是我的密码: import test_module as module print("\nAssignment 4") def main(): """ Collects fir

我的任务是检查是否输入了姓名和年龄,如果为空,则再次提示用户输入

当用户输入一个空输入时,它会进入年龄输入,然后它会正确提示,但是如果用户输入了非有效输入,然后再次输入空白输入,则不会再次给他们空白输入提示,只是非有效输入。我不知道如何解决这个问题

这是我的密码:

import test_module as module
 print("\nAssignment 4")

def main():

    """
    Collects first name input, checks if input was blank and prompts for entry until entered.
    """
    firstname=input("\nPlease enter your first name. ")
    while module.is_field_blank(firstname)==False:
        print("\nFirst name must be entered.")
        firstname=input("\nPlease enter your first name. ")
        continue
    """
    Collects last name input, checks if input was blank and prompts for entry until entered.
    """
    lastname=input("\nPlease enter your last name. ")
    while module.is_field_blank(lastname)==False:
        print("\nLast name must be entered.")
        lastname=input("\nPlease enter your last name. ")
        continue
    """
    Collects age input, checks if field is blank, if field is not blank checks if field is a number, prompts for entry until entered for both
    """
    raw_age=input("\nWhat is your age? ")
    while module.is_field_blank(raw_age)==False:
        print("\nAge must be entered.")
        raw_age=input("\nwhat is your age? ")
        continue
    while module.is_field_a_number(raw_age)==False:
        print("\nAge must be a number.")
        raw_age=input("\nWhat is your age? ")
        continue
    """
    Changes raw_age string to age integer
    """
    age=int(raw_age)
    """
    Brigns all inputs together, checks against age integer and outputs depending.
    """
    if age>40:
        print("\nWell, "+firstname+" "+lastname+" it looks like you are over the hill.")
    else:
        print("\nIt looks like you have many programming years ahead of you "+firstname+" "+lastname)
        

if __name__ == "__main__":
    
     main()

print("\nEnd of assignment 4")
这是我的模块代码:

def is_field_blank(arg):
    """
    This checks if the field is blank
    """
    if arg == "":
        return False
    else:
        return True

def is_field_a_number(yrs):

    """
    This checks if the entered field is a number or not
    """
    if yrs.isdigit() !=True:
        return False
    else:
        return True

您可以使用带有检查和中断的while-True循环来迭代提示列表并返回值

def prompt2ints(*args):
    vals = []
    for i in args:
        while True:
            val = input(i)
            if val == "": print("you must enter a value")
            try:
                if int(val): vals.append(val); break
            except ValueError: print("value needs to be int")
    return vals

print(prompt2ints("one prompt here: ", "something here too: ")
输出:

enter age: 10
something here too: 20
['10', '20']

嗨,欢迎!我很想帮忙,但是你的帖子太难读了。我建议你编辑你的文章,并将代码部分重新格式化为“代码”。只需突出显示文本并单击{}按钮即可。这是否回答了你的问题@Kayla0x41对此很抱歉,这应该更具可读性。所以本质上,您只想循环,直到从用户那里获得2个有效值?如果你没有,那么提示他们再次输入?@Ironkey正确。输入必须是实际整数,而不是空的。它已经正确地执行了提示,但如果输入非整数后输入空白,它不会返回并给出错误输出。例如:“您的年龄是多少?(用户不输入任何内容)必须输入年龄。您的年龄是多少?(用户输入ff)年龄必须是一个数字。您的年龄是多少?(用户输入空白)这里是搞乱的地方。年龄必须是一个数字。”它应该吐出“您必须输入您的年龄”
import module

# Declaring the firstname lastname and raw_age type so that I can use global on them 

firstname = str
lastname = str
raw_age = int

# Defining functions for each of the properties

def firstnameFunc():
    # making it global so that I can use it in the end of the while loop
    global firstname
    firstname = input("\nPlease enter your first name. ")
    if module.is_field_blank(firstname) == False:
        print("\nFirst name must be entered.")
        # if the field is blank then it will re-run this function and prompt again 
        firstnameFunc()

def lastnameFunc():
    # just globalizing it for use at the end of the while loop
    global lastname
    lastname = input("\nPlease enter your last name. ")
    if module.is_field_blank(lastname) == False:
        print("\nLast name must be entered.")
        # of it's blank it will recall this function to prompt user again
        lastnameFunc()

def ageFunc():
    # globalizing it for the end of the while loop
    global raw_age
    raw_age = input("\nwhat is your age? ")
    if module.is_field_blank(raw_age) == False:
        print("\nAge must be entered.")
        # if it's blank it will recall the function
        ageFunc()
    elif module.is_field_a_number(raw_age) == False:
        print("\nAge must be a number.")
        # if it's not an integer it will recall the function
        ageFunc()

def main():   
    # NOTE! you don't actually need this while loop. It's useless
    while True: 
    # Collects first name input, checks if input was blank and prompts for entry until entered.
        firstnameFunc()

    # Collects last name input, checks if input was blank and prompts for entry until entered.
        lastnameFunc()
    
    # Collects age input, checks if field is blank, if field is not blank checks if field is a number, prompts for entry until entered for both
        ageFunc()
    
    # Changes raw_age string to age integer
        age = int(raw_age)
    
        # Brigns all inputs together, checks against age integer and outputs depending.
        if age>40:
            print("\nWell, "+firstname+" "+lastname+" it looks like you are over the hill.")
        else:
            print("\nIt looks like you have many programming years ahead of you "+firstname+" "+lastname)
        break
        # I would recommend getting rid of the while loop
    
if __name__ == "__main__":

    main()
    print("\nEnd of assignment 4")