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?

asked Jan 21, 2014 at 15:46
9
  • your not saving out the int function. try you_var =int(Number1,Number2) Commented Jan 21, 2014 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) Commented Jan 21, 2014 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. Commented Jan 21, 2014 at 15:48
  • 1
    also Number1 = int(input(...)) Commented Jan 21, 2014 at 15:48
  • 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. Commented Jan 21, 2014 at 15:49

5 Answers 5

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.

answered Jan 21, 2014 at 15:57
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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'>
answered Jan 21, 2014 at 15:55

Comments

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.
answered Jan 21, 2014 at 15:56

1 Comment

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.
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
answered Jan 21, 2014 at 15:56

5 Comments

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)?
@user3216729, yes but you need to do Option=int(Option)
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!
@user3216729, you could upvote or accept it if you like this answer. ;)
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 :/
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!

answered Jan 21, 2014 at 16:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.