0

I have looked on here for an idea in order to loop the user's invalid input and I haven't found one.

Ok, I know how to loop the first_number and second_number together but I am a bit confused on how to loop the separately if need be. So if the user inputs a bad second_number and loops that instead of the whole thing again.

I've attached the part I need help with (yes this is a school assignment):

def get_numbers():
first_number = 0.0
second_number = 0.0
while True:
 try:
 first_number = float(input("Please enter the first number: "))
 second_number = float(input("Please enter the second number: "))
 except ValueError:
 print("Not a valid input, please try again.")
 continue
 return first_number, second_number
asked Dec 8, 2017 at 2:41

2 Answers 2

2

To use 1 loop, you may need to recognize the differences:

  1. You use 2 simple variables for the results though you could use 1 list.
  2. The input string is almost the same except for "first" and "second" words.

Concept:

  1. First you want a list.
  2. Then use a for loop to use "first", then "second" words.
  3. Then use a while loop which processes the inputs and uses the list to extend with the replies. Use break to get out of the while loop after each good reply.

def get_numbers():
 result = []
 for item in ("first", "second"):
 while True:
 try:
 number = float(input("Please enter the " + item + " number: "))
 except ValueError:
 print("Not a valid input, please try again.")
 else:
 result += [number]
 break
 return tuple(result)

Returning as a tuple as you have done.

answered Dec 8, 2017 at 5:57
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for such a late reply. I understand what you are doing here and it broke down the code so it is shorter (always nice). The thing I need to learn a bit more is tuple. Other than that, thank you for your response and breaking it down in simpler terms.
0

First, you want to use two loops, one for each number, and break if the input is valid instead of continuing if it's invalid. Then you need to move the return statement to immediately following the second loop.

By the way, you can leave out the initial assignments to first_number and second_number (commented out below). Assigning them to float(input(...)) is enough.

def get_numbers():
 #first_number = 0.0
 #second_number = 0.0
 while True:
 try:
 first_number = float(input("Please enter the first number: "))
 break
 except ValueError:
 print("Not a valid input, please try again.")
 while True:
 try:
 second_number = float(input("Please enter the second number: "))
 break
 except ValueError:
 print("Not a valid input, please try again.")
 return first_number, second_number
answered Dec 8, 2017 at 2:54

1 Comment

Thank you for your reply and I apologize for my late one. Now, I wanted to go down this road but I didn't know where to begin. I thought to put in 2 while loops would create confusion so I didn't even attempt it. Also, I understand what you meant by leaving out the initial 2 assignments. Thank you for your feedback.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.