Python Input Validation Fractional

Viewed 31

Working on my basic Python Programming and I can't seem to solve this problem. I have solved the ability to terminate the program and display an error message. However, I can not get the program to do my calculations if the inputs are fractional values.

Write program that calculates the miles per gallon achieved on a road trip. Ask the user, and accept input of miles driven on the trip, and the Gallons of gasoline used. Expect the user to input fractional values like 243.5 miles or 22.45 gallons. Calculate and display the miles per gallon achieved on the trip. Before you make any calculations make sure that the input is valid. If input is not valid, print an error message and terminate.

# Ask user to input miles driven on trip
miles = float(input("How many miles were driven: "))

# Ask user to input gallons of gasoline used
gasoline = float(input("How many gallons of gasoline were used: "))

whole_number = int

mpg = miles / gasoline

if miles or gasoline == whole_number:
    print("Input is not valid. Please input a fractional value")
else:
    print(round(mpg, 2), "mpg used on this road trip")
print("End of program")```
1 Answers

I don't think the assignment expects you to reject whole numbers (surely it's possible to drive a whole number of miles on a trip), only that you should be sure not to assume that inputs are integers. Just remove your whole_number check:

# Ask user to input miles driven on trip
miles = float(input("How many miles were driven: "))

# Ask user to input gallons of gasoline used
gasoline = float(input("How many gallons of gasoline were used: "))

mpg = miles / gasoline
print(f"{mpg:.2f} mpg used on this road trip")

There are three reasons that this code didn't work:

whole_number = int
if miles or gasoline == whole_number:
  • A float with no fractional component is still a float, not an int.
  • Comparing a variable for equality with the int type is not how to check whether the variable is an instance of an int. Instead you'd do something like isinstance(miles, int).
  • A statement like x or y == z isn't checking if either x or y is equal to z. Instead it's checking (x) or (y == z) -- i.e. it's checking whether x is true (truthy) or if y equals z. Any non-zero number is truthy, so even though gasoline == whole_number can never be true (for both of the reasons described above) miles or gasoline == whole_number will be true as long as miles isn't zero. The way to check if x or y equals z in Python would be either x == z or y == z, or something like z in {x, y}.

If you did want to check whether a floating point number is a "round" number (i.e. no fractional component within the bounds of floating point imprecision), you can take advantage of the fact that a round float equates to its int conversion:

>>> x = float("3")
>>> type(x)
<class 'float'>
>>> isinstance(x, int)
False
>>> x == int(x)
True

Note that the type of x is float even though the string it was generated from had no decimal point, but x == int(x) because x is a round number. Compare to:

>>> 3.1 == int(3.1)
False
Related