-1

So I just wrote a basic mile to km converter on python3. It asks you If you want to convert nautical miles or land miles into km when you type nautical it goes to that loop and converts nautical miles to km. The same thing goes with land, if you type land it converts land miles into km. But the problem is when I type something else than nautical or land the program just crashes. How can I write the code that says "Please only write nautical or land in this input".And when I type a input that is not a number in the "how many miles do you want to convert in km part" the same thing happens and it crashes...

print("Hello! This is a mile to km converter")
print("Do you want to convert nauitcal miles or land miles?")

question_convert = input("Please type nautical or land \n")

if question_convert == "nautical":
NAUTICAL = True

if question_convert == "land":
    NAUTICAL = False

while True:
    while NAUTICAL == True:
        nautical_mile = 1.852
        print("How many nauitical miles do you want to convert in km?")
        convert_nauitcal = float(input())
        converted_nautical = float(convert_nauitcal * nautical_mile)
        print("Here's your converted nauitical miles: ")
        print(converted_nautical)
        quit()

    while NAUTICAL == False:
        land_mile = 1.609344
        print("How many land miles do you want to convert in km?")
        convert_land = float(input())
        converted_land = float(convert_land * land_mile)
        print("Here's your converted land miles: ")
        print(converted_land)
        quit()

#land_mile = 1.609344
#nautical_mile = 1.852
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Boran
  • 37
  • 1
  • 6
  • Get rid of the two while statesments, and use an if and elif statements. And under the `print(converted_land)`, use break instead. – MichaelRSF Aug 22 '18 at 21:15

4 Answers4

1

I would change it and put the question part also inside of a while statement so that your user can't continue until they have input a valid response. So, adjusted code would look like:

print("Hello! This is a mile to km converter")
print("Do you want to convert nauitcal miles or land miles?")

valid_input=False
while not valid_input:
    question_convert = input("Please type nautical or land \n")
    if question_convert == "nautical":
        NAUTICAL = True
        valid_input=True
    if question_convert == "land":
        NAUTICAL = False
        valid_input=True

while True:
    while NAUTICAL == True:
        nautical_mile = 1.852
        print("How many nauitical miles do you want to convert in km?")
        convert_nauitcal = float(input())
        converted_nautical = float(convert_nauitcal * nautical_mile)
        print("Here's your converted nauitical miles: ")
        print(converted_nautical)
        quit()

    while NAUTICAL == False:
        land_mile = 1.609344
        print("How many land miles do you want to convert in km?")
        convert_land = float(input())
        converted_land = float(convert_land * land_mile)
        print("Here's your converted land miles: ")
        print(converted_land)
        quit()
cassie
  • 46
  • 5
0

This is because you throw it into a an infinite while loop. You can fix this by modifying your initial if conditions, for when NAUTICAL is neither True nor False:

print("Hello! This is a mile to km converter")
print("Do you want to convert nauitcal miles or land miles?")

question_convert = input("Please type nautical or land \n")

NAUTICAL = None
if question_convert == "nautical":
    NAUTICAL = True    
else if question_convert == "land":
    NAUTICAL = False
else:
    print("I don't understand that...")
    # maybe throw an exception here if you want, instead of exit()-ing
    exit()

while NAUTICAL is not None:
    while NAUTICAL:
        nautical_mile = 1.852
        print("How many nauitical miles do you want to convert in km?")
        convert_nauitcal = float(input())
        converted_nautical = float(convert_nauitcal * nautical_mile)
        print("Here's your converted nauitical miles: ")
        print(converted_nautical)
        quit()

    while not NAUTICAL:
        land_mile = 1.609344
        print("How many land miles do you want to convert in km?")
        convert_land = float(input())
        converted_land = float(convert_land * land_mile)
        print("Here's your converted land miles: ")
        print(converted_land)
        quit()

#land_mile = 1.609344
#nautical_mile = 1.852
David Culbreth
  • 2,610
  • 16
  • 26
0

For your first input problem it's probably crashing because the variable NAUTICAL isn't defined unless the input is 'nautical' or 'land'.

One was to solve this is

if question_convert=='nautical':
   NAUTICAL = True
elif question_convert=='land':
   NAUTICAL = False
else:
   print('Please type land or nautical!!')
   sys.exit(1)

For the other input validation problem the easiest way to check for numerical input in a case like this is to use a try/except clause around the float conversion

...
in_val = input()
try:
   converted_land = float(in_val)
except ValueError:
   print("{} doesn't look like a number!!".format(inv_val))
   # might want to exit at this point or use a default value for converted_land
...
Jon Sorenson
  • 136
  • 5
0
ALLOWED_TYPES = {'nautical', 'land'}    

question_convert = ''

while question_convert not in ALLOWED_TYPES:

    question_convert = input("Please type nautical or land \n")

    if question_convert not in ALLOWED_TYPES:

        print('"Please only write nautical or land in this input')
        continue

    if question_convert == "nautical":
        NAUTICAL = True

    if question_convert == "land":
        NAUTICAL = False
Felipe Emerim
  • 363
  • 2
  • 12