I have searched the forums and could not find a direct answer to my question, it's probably too simple and i'm over looking something.

It's my first day with Python, I'm in the process of developing a program that converts a given float integer into a variety of different things.

a raw_input asks the user to enter a positive number.

When the user enters a letter, negative number, or a #/0, you get a syntax error.

I'm looking to make it tell the user that the value entered is not a proper number and to try again, instead of getting a syntax error.

How do I go about doing so?

For reference, here's the code I have so far:

numStr1 = raw_input ('Please input a number representing gallons of gasoline: ')
float1 = float(numStr1)

...part i'm stuck on:
while True:
if len(numStr1) < 0:
continue # Please enter a positive number

back to proper code

print float1, 'gallons is the equivalent of:',float1*3.7854,'liters'
etc etc

Any help guys? This is an assignment, i'll admit it, but I cannot find a straight answer anywhere.

You sure thats a syntax error and not a type error? Anyway, you need to check that numstr is actually a float first before calling float() on it. Or, you can just be lazy and do it like me.

    numstr = float(numstr)
    print numstr, "is not a valid floating point number."

What you gave me does give me the proper feed stating that the number entered is not a valid floating point, however, I am wanting it to only say that when the number is indeed not correct.

raw_input() is for strings. Try input()...

commented: Input() is way too dangerous for a newbie to use. +0

raw_input() is for strings. Try input()...

Never use input(), always consider the user may accidentally put in a string character. That way you can handle it appropriately.

Anyways csm_tC, you need to use a try & except block.

Here's an example that works fine. I hope you don't have too much trouble putting it in a loop if you wish.

gas = raw_input ('Please input a number representing gallons of gasoline: ')
liters = -50

    gas = int(gas)
    liters = gas * 3.7854
    print gas, 'gallons is the equivalent of:', str(liters), 'liters'
    print "You entered an incorrect amount of gas!!!!!"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.