Foysal94
Foysal94

Reputation: 603

How to parse user input as a number

I am making a calculator program in my Python, following a tutorial. Here is my code:

print ("This is a calculator program, press Enter to continue")
a = input()

while a == "":
    print("Enter 1 for option 1 which adds")
    print("Enter 2 for option 2 which subtracts")
    print("Enter 3 for option 3 which multiply")
    print("Enter 4 for option 4 which divides")
    print("Enter 5 for option 5 which quits",)

    Option = input("Enter an option number:")
    int(Option)

    if Option == 1:
        Number1 = input("Enter number 1")
        Number2 = input("Enter number 2")
        int(Number1,Number2)
        print(Result = Number1 + Number2)

    if Option == 2:
        Number1 = input("Enter number 1")
        Number2 = input("Enter number 2")
        int(Number1,Number2)
        print(Result = Number1 - Number2)

    if Option == 3:
        Number1 = input("Enter number 1")
        Number2 = input("Enter number 2")
        int(Number1,Number2)
        print(Result = Number1 * Number2)

    if Option == 4:
        Number1 = input("Enter number 1")
        Number2 = input("Enter number 2")
        int(Number1,Number2)
        print(Result = Number1 / Number2)

    if Option == 5:
        break

It is very basic, it gets up to the point of printing all the option numbers and then asks me to pick one. So I enter "1" as a string, parsing it to an integer 1. However it doesn't go straight to option 1 and instead loops again which is fine I will sort that out later. But again it doesn't go to any option when I enter 1-5. I think I typed in the wrong code to parse it or something?

Upvotes: 0

Views: 2285

Answers (5)

chronologos
chronologos

Reputation: 338

I corrected your code.

_ = input("This is a calculator program, press Enter to continue")
print ("""Enter 1 for option 1 which adds
          Enter 2 for option 2 which subtracts
          Enter 3 for option 3 which multiplies
          Enter 4 for option 4 which divides
          Enter 5 for option 5 which quits""")
while True:
    Option = input("Enter an option number: ")

    if Option == '1':
        Number1 = int(input("Enter number 1: "))
        Number2 = int(input("Enter number 2: "))
        print("The Result is {0}".format(Number1 + Number2))

    elif Option == '2':
        Number1 = int(input("Enter number 1: "))
        Number2 = int(input("Enter number 2: "))
        print("The Result is {0}".format(Number1 - Number2))

    elif Option == '3':
        Number1 = int(input("Enter number 1: "))
        Number2 = int(input("Enter number 2: "))
        print("The Result is {0}".format(Number1 * Number2))

    elif Option == '4':
        Number1 = int(input("Enter number 1: "))
        Number2 = int(input("Enter number 2: "))
        print("The Result is {0}".format(Number1 / Number2))

    else:
        break

Notes:

  1. Triple quote syntax is good for long multiline strings.
  2. The pythonic way of formatting a printed string is the str.format method.

Good luck learning!

Upvotes: 0

Christian Tapia
Christian Tapia

Reputation: 34146

input() converts the input to a string, so if you need to read an int, you have to cast it.

In the if condition, you could cast the input() result (a string) to int:

Number1 = int(input("Enter number 1"))

then create a variable, let's say result and assign it the sum of the numbers:

result = Number1 + Number2

and finally print the result

print "Result = " + str(result)

The final code should look like this:

print ("This is a calculator program, press Enter to continue")
a = input()

while a == "":
    print
    print("Enter 1 for option 1 which adds")
    print("Enter 2 for option 2 which subtracts")
    print("Enter 3 for option 3 which multiply")
    print("Enter 4 for option 4 which divides")
    print("Enter 5 for option 5 which quits",)

    Option = input("Enter an option number:")

    if Option == 1:
        Number1 = int(input("Enter number 1"))
        Number2 = int(input("Enter number 2"))
        result = Number1 + Number2
        print "Result = " + str(result) # To print you have to cast to `str`

    elif Option == 2:
        ...
    elif Option == 3:
        ...
    elif Option == 4:
        ...
    else:
        break

Notes:

  • You could use an if-elif-else as the structure, so if Option == 1, the following conditions won't be checked.

  • I would also recommend you to follow Python naming convention. Your variable Number1 should be called number1 and so on.

Upvotes: 1

zhangxaochen
zhangxaochen

Reputation: 33997

that code posted contains several errors, below is the corrected code:

print ("This is a calculator program, press Enter to continue")
a = input()

while a == "":
    print("Enter 1 for option 1 which adds")
    print("Enter 2 for option 2 which subtracts")
    print("Enter 3 for option 3 which multiply")
    print("Enter 4 for option 4 which divides")
    print("Enter 5 for option 5 which quits",)

    Option = int(input("Enter an option number:"))

    if Option == 1:
        Number1 = int(input("Enter number 1"))
        Number2 = int(input("Enter number 2"))
        # int(Number1,Number2)
        Result = Number1 + Number2

    if Option == 2:
        Number1 = int(input("Enter number 1"))
        Number2 = int(input("Enter number 2"))
        # int(Number1,Number2)
        Result = Number1 - Number2

    if Option == 3:
        Number1 = int(input("Enter number 1"))
        Number2 = int(input("Enter number 2"))
        # int(Number1,Number2)
        Result = Number1 * Number2

    if Option == 4:
        Number1 = int(input("Enter number 1"))
        Number2 = int(input("Enter number 2"))
        # int(Number1,Number2)
        Result = Number1 / Number2

    print(Result)

    if Option == 5:
        break

Upvotes: 0

senshin
senshin

Reputation: 10350

Your code should look more like this:

print("This is a calculator program. Press Enter to continue.")

while True:
    _ = input()
    print("Enter 1 for option 1 which adds")
    print("Enter 2 for option 2 which subtracts")
    print("Enter 3 for option 3 which multiply")
    print("Enter 4 for option 4 which divides")
    print("Enter 5 for option 5 which quits")

    option = int(input("Enter an option number: "))

    if option == 5:
        break
    else:
        number1 = int(input("Enter number 1: "))
        number2 = int(input("Enter number 2: "))
        if option == 1:
            result = number1 + number2
        elif option == 2:
            result = number1 - number2
        elif option == 3:
            result = number1 * number2
        elif option == 4:
            result = number1 / number2
        print(result)

Salient points:

  • You aren't doing anything with a. So I got rid of it, and put a call to input that stores its result in _, which is the standard name for a variable whose value you don't care about.
  • You must explicitly convert option to an int. Python will not implicitly convert for you, and so '1' != 1.
  • You cannot convert to an int in-place - writing int(number1) does nothing. You must write number1 = int(number1) or similar.
  • You cannot convert multiple strings to an int in a single statement of the form int(number1, number2). What you're actually doing here is calling int(x, base), where you convert x into an int, interpreted as being in base base.
  • I refactored your if statements to be more concise
  • Variable names are typically lowercase in Python.
  • You cannot assign to a variable inside a print statement.

Upvotes: 0

jb.
jb.

Reputation: 23955

Result of input function is a string, you need to convert it to int, using int type .

>>> foo = "3"
>>> foo
'3'
>>> int(foo)
3

Your misconception might come from that python is a dynamically typed language. But remember that despite variables themselves are untyped, variable values have types.

>>> type(foo)
<class 'str'>
>>> type(int(foo))
<class 'int'>

Upvotes: 0

Related Questions