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?
5 Answers 5
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-elseas the structure, so ifOption == 1, the following conditions won't be checked.I would also recommend you to follow Python naming convention. Your variable
Number1should be callednumber1and so on.
1 Comment
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.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'>
Comments
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 toinputthat stores its result in_, which is the standard name for a variable whose value you don't care about. - You must explicitly convert
optionto anint. Python will not implicitly convert for you, and so'1' != 1. - You cannot convert to an
intin-place - writingint(number1)does nothing. You must writenumber1 = int(number1)or similar. - You cannot convert multiple strings to an
intin a single statement of the formint(number1, number2). What you're actually doing here is callingint(x, base), where you convertxinto anint, interpreted as being in basebase. - I refactored your
ifstatements to be more concise - Variable names are typically lowercase in Python.
- You cannot assign to a variable inside a print statement.
1 Comment
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
5 Comments
Option=int(Option)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:
- Triple quote syntax is good for long multiline strings.
- The pythonic way of formatting a printed string is the str.format method.
Good luck learning!
you_var =int(Number1,Number2)a = input()should be inside the while loop, and also instead ofprint(Result = Number1 + Number2)you shouldprint(Number1 + Number2)(similarly for the other options)Number1 = int(input(...))Option = 1. Option will be astrand 1 is anint. It'll always return false.