0

So for the past hour or so i've been toiling over this piece of work. I want to add a while loop that brings the user back to the start evertime they input something that isn't a number. However I have no idea where to start on this. I've been trying a whole ton of things but nothing seems to be working for me.

try:
    length=float(raw_input("Insert Length of room"))
except ValueError:
    print "That's not a number try again"
try:
    width=float(raw_input("Insert Width of room"))
except ValueError:
    print "That's not a number try again"
else:
    cost=(width*length)*3.99
 print cost
noel pearce
  • 69
  • 1
  • 1
  • 4
  • Have you tried this: indent the whole logic inside a `while True:` and add a `continue` statement when you want to redo from the while, a `break` statement when you want to exit the loop...? – Alex Martelli Dec 17 '14 at 02:35
  • Who downvoted every single answer? – nbro Dec 17 '14 at 02:48

4 Answers4

0

You can use while and break:

length = 0
while True:
    try:
        length = float(raw_input("Insert Length of room: "))
        break
    except ValueError:
        print "That's not a number try again"

# You'll have a valid float in length at this point
print(length)
miku
  • 181,842
  • 47
  • 306
  • 310
0

You could do something like this:

dimensions = ['Length', 'Width']
values = []

for dimension in dimensions:
    while True:
        try:
            v = float(raw_input("Insert {} of room: ".format(dimension)))
            values.append(v)
            break
        except ValueError:
            print "That's not a number try again"

length, width = values

cost = (width * length) * 3.99
print cost

Edited for updated requirements.

Aidan Kane
  • 3,856
  • 2
  • 25
  • 28
  • That works the issue is I don't want it to start over again at length if it doesn't get a valid float for width. – noel pearce Dec 17 '14 at 01:51
0

If you want to do the check for multiple inputs, you might define a function to get the input and check it:

def numeric_input(message, numeric_type=float):
    while True:
        try:
            return numeric_type(raw_input(message))
        except ValueError:
            print "That isn't a valid number! Try again."

Then:

length = numeric_input("Insert Length of room")
width = numeric_input("Insert Width of room")
cost = (width*length)*3.99
print cost

That way if the user gives a number for the length, but some non-numeric input for the width, the program won't ask the user to input the length again, only the width.

jme
  • 19,895
  • 6
  • 41
  • 39
-1

YOu can do as follows:

while True:
    try:
        length = float(raw_input("Insert Length of room: "))
    except ValueError:
        print "That's not a number try again"
    else:
        break

If you have multiple questions, you can do as follows:

in_data = {
           'length': ['Insert Length of room: ', -1], # the -1 is placeholder for user input
           'width': ['Insert Width of room: ', -1], 
           }


for k,v in in_data.items():
    while True:
        try:
            user_data = float(input(v[0]))            
        except ValueError:
            print("That's not a number try again")
        else:
            v[1] = user_data
            break

print(in_data)   
# example output: {'width': ['Insert Width of room: ', 7.0], 'length': ['Insert Length of room: ', 8.0]}
Marcin
  • 215,873
  • 14
  • 235
  • 294