1

I'm developing a simple conversion program, but I got stuck on preventing the user from typing in a string where integers are expected:

    choicecheck = 1 
    inputcheck = 1 
    while choicecheck == 1: 
        choice = input ("""please choose type of convert:
        1. Centimeters to inches
        2. Inches to centimeters
        3. Exit""") 


        while inputcheck == 1:
            if choice == "1":
                cm = int(input ("Please type in value in centimeters.")) 
                if type(cm) == int:
                    cmsum = cm * 0.39 # multiplies user input by 0.39
                    print (cm, " centimeters is ", cmsum, " inches") 
                    choicecheck = 0
                    inputcheck = 0
                else:
                     print("Sorry, invalid input")

When it comes to inputcheck, the else part of the if statement does not work, and I have no idea why is that. Please help.

1
  • type(cm) can only ever be int because cm was constructed using a call to int() on the user's input. But that call fails if the user happens to enter something that can't be interpreted as an integer. For this, you use a try/except statement like the one @mhlester proposes. Type checking is rarely ever done in Python. Commented Feb 6, 2014 at 20:38

2 Answers 2

3
while True:
    try:
        choice = int(input('please choose type of convert'))
    except ValueError:
        # user entered an input that couldn't be converted to int
        continue
    else:
        if not 1 <= choice <= 3:
            # valid int, invalid choice
            continue
        # success. stop looping
        break

Now you have an int that represents their input. It will continue asking until they've successfully entered an int

Sign up to request clarification or add additional context in comments.

Comments

0

This will keep the program looping until the user selects to exit. I used a float for the second user input since the user may need to find a value for 2.5 inches or something similar.

while True:
    try:
        choice = int(input("""please choose type of convert:
        1. Centimeters to inches
        2. Inches to centimeters
        3. Exit"""))

        if choice == 1:
            while True:
                 try:
                    cm = float(input ("Please type in value in centimeters.")) 
                    cmsum = cm * 0.39 # multiplies user input by 0.39
                    print (cm, " centimeters is ", cmsum, " inches")
                    break
                 except ValueError:
                    print('error')

        elif choice ==2:
            while True:
                try:
                    stuff()
                    break
                except ValueError:
                    print('error')

        elif choice == 3:
            print('Thanks!')
            break

    except ValueError:
        print('error')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.