0

I have this assignment to check if names and an age are input, if they're blank it prompts the user for the input again.

When it gets to the age input if the user enters a blank input then it will prompt them correctly but if a user enters a non-valid input and then enters a blank input again it does not give them the blank input prompt again, just the non-valid input. I'm unsure how to fix this problem.

Here is my code:

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")

Here is my module code:

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
prosoitos
  • 6,679
  • 5
  • 27
  • 41
177013
  • 3
  • 2
  • Hi, and welcome! I'd like to help, but your post is pretty unreadable. I would recommend you edit your post, and reformat the code sections as "code." It's as simple as highlighting the text and clicking the {} button. – Kayla0x41 Oct 02 '20 at 16:23
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Random Davis Oct 02 '20 at 16:25
  • 1
    @Kayla0x41 Sorry about that, that should be more readable. – 177013 Oct 02 '20 at 16:27
  • so essentially, you just want to loop until you get 2 valid values from the user? and if you don't then prompt them to enter again? – Ironkey Oct 02 '20 at 17:01
  • @Ironkey Correct. The inputs need to be both an actual integer and not blank. It already does the prompting correctly, but it doesn't go back and give the error output if they enter a blank after entering a non-integer. An example is like this: "What is your age? (user inputs nothing) Age must be entered. What is your age? (User inputs ff) Age must be a number. What is your age? (User inputs blank) *here is where it messes up* Age must be a number." It should spit out "You must enter your age" – 177013 Oct 02 '20 at 17:50
  • @177013 ok that's simple enough ill submit an answer when I have some time! thanks for clarifying. – Ironkey Oct 02 '20 at 18:00

2 Answers2

0

You could use a while True loop with checks and breaks to iterate through a list of prompts and the return the values

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: ")

output:

enter age: 10
something here too: 20
['10', '20']
Ironkey
  • 2,568
  • 1
  • 8
  • 30
0
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")
FastFinge
  • 54
  • 3