0

So I am making a program that takes kilometers and converts it to miles and then the other way around as well. When the program runs, the user is first prompted to enter either the number 1 or 2 to determine whether its kilo to miles or miles to kilo. If a user inputs anything other than a 1 or 2 I want to program to display "Please enter either the value 1 or 2" etc. and then ask again. What are some ways of doing that? Here is my code so far.

#Program that takes kilometers and converts it to miles VV
def main():
    choice = input("Kilometers to Miles/Miles to Kilometers? (1, 2): ")

    if choice == 1:
        kilo = float(input("How many kilometers will you be traveling?: "))
        convert = 0.62
        miles = kilo * convert
        print("You will be traveling about", miles, "miles!")
    else:

        milesTraveled = eval(input("How many miles will you be traveling?: "))
        convertTwo = milesTraveled // 0.62
        print("You will be traveling about", convertTwo, "kilometers!")
#end main
main()
max
  • 2,757
  • 22
  • 19

3 Answers3

0

The easiest way would be to use if statements.

user_input = input('Please enter either the value 1 or 2: ')
if(user_input == 1 or user_input == 2):
    # do stuff here (remove the pass and replace with code you want to execute)
    pass
else:
    user_input = input('Please enter either the value 1 or 2: ')

edit since code was provided

Just replace your else block with:

elif(choice == 2):
    milesTraveled = eval(input("How many miles will you be traveling?: "))
    convertTwo = milesTraveled // 0.62
    print("You will be traveling about", convertTwo, "kilometers!")

Then add:

else:
    choice = input('Please enter either the value 1 or 2: ')
Matthew Varga
  • 405
  • 4
  • 14
  • This approach will check for an invalid input and ask you again only once. If you want the program to keep asking you until you give it a valid value, you want to use a while loop. – Reti43 Mar 15 '16 at 10:55
0

You can do like this;

try:
    choice = int(raw_input("Enter choice 1 or 2:"))
    if choice not in [1, 2]:
        raise ValueError()
except ValueError:
    print "Invalid Option, you needed to type a 1 or 2"
else:
    print "Your choice is", choice
Geo Jacob
  • 5,909
  • 1
  • 36
  • 43
0

You may use if .. elif .. else .. construction like this:

#Program that takes kilometers and converts it to miles VV
def main():
    choice = input("Kilometers to Miles/Miles to Kilometers? (1, 2): ")

    if choice == 1:
        kilo = float(input("How many kilometers will you be traveling?: "))
        convert = 0.62
        miles = kilo * convert
        print("You will be traveling about", miles, "miles!")
    elif choice == 2:
        milesTraveled = eval(input("How many miles will you be traveling?: "))
        convertTwo = milesTraveled // 0.62
        print("You will be traveling about", convertTwo, "kilometers!")
    else:
        print("You have entered incorrect value! Please try again\n")
        main()
#end main
main()
max
  • 2,757
  • 22
  • 19
  • For some reason after adding the elif statement no matter what value I enter it prints "You have entered incorrect value! Please try again". Probably a simple syntax error but thanks! Just what I was looking for. –  Mar 13 '16 at 07:05