0

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?

Foysal94
  • 565
  • 2
  • 7
  • 15
  • your not saving out the int function. try `you_var =int(Number1,Number2)` – agconti Jan 21 '14 at 15:48
  • `a = input()` should be inside the while loop, and also instead of `print(Result = Number1 + Number2)` you should `print(Number1 + Number2)` (similarly for the other options) – Gabi Purcaru Jan 21 '14 at 15:48
  • 4
    You should *really* use a better title. Almost every beginner in Python *just* started learning, but that doesn't mean that your question would be useful to them. – Martijn Pieters Jan 21 '14 at 15:48
  • 1
    also `Number1 = int(input(...))` – Gabi Purcaru Jan 21 '14 at 15:48
  • I believe your also looking for `raw_input()` – agconti Jan 21 '14 at 15:49
  • @agconti the OP seems to be using Python3, where it's `input`, not `raw_input` AFAIK – Gabi Purcaru Jan 21 '14 at 15:49
  • 1
    I think your main problem is the comparison `Option = 1`. Option will be a `str` and 1 is an `int`. It'll always return false. – Paulo Bu Jan 21 '14 at 15:49
  • Welcome to StackOverflow! Could you take a look at the guidelines for asking a question - http://stackoverflow.com/questions/how-to-ask and consider editing your title to be more meaningful. We look forward to you contributing to the community! – Wolfwyrd Jan 21 '14 at 15:50
  • +1 for a very much improved question after the edit. – tripleee Jan 21 '14 at 16:50

5 Answers5

1

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.

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • There is no `raw_input` in Python 3, which OP seems to be using. Python 3 `input` behaves like Python 2 `raw_input`, and there is no Python 3 equivalent of Python 2's `input`. – senshin Jan 21 '14 at 15:58
0

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'>
jb.
  • 23,300
  • 18
  • 98
  • 136
0

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.
senshin
  • 10,022
  • 7
  • 46
  • 59
  • Hey do you know where you said Option = int(input("Enter an option number:")), is it possible to do it like this Option = input("Enter an option number") /newline int(Option)? I forgot to put that in my orignal post, the one line of code. – Foysal94 Jan 21 '14 at 16:28
0

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
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • Hey do you know where you said Option = int(input("Enter an option number:")), is it possible to do it like this Option = input("Enter an option number") /newline int(Option)? – Foysal94 Jan 21 '14 at 16:23
  • @user3216729, yes but you need to do `Option=int(Option)` – zhangxaochen Jan 21 '14 at 16:25
  • ah right ty, I come from C# so thats why I would do it like that. I tend to go for 1 or 2 more lines instead of doing all in one line for me personally, jsut helps me look over it.Thanks! – Foysal94 Jan 21 '14 at 16:29
  • @user3216729, you could upvote or accept it if you like this answer. ;) – zhangxaochen Jan 21 '14 at 16:30
  • C# is the only language I know so far, and in C# I would do it this way Console.Write("Enter an option number") /newline string option = Console.ReadLine() /newline optionnumber = int.Parse(option). Is an input() just the same as a Console.Write()/Console.WriteLine()? It doesnt let me upvote :/ – Foysal94 Jan 21 '14 at 16:34
0

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!

Community
  • 1
  • 1
chronologos
  • 338
  • 3
  • 13