Posts: 7
Threads: 2
Joined: Jul 2018
When i enter both numbers it doesn't output calculation, it just skips to 'invalid'. Is there some sort of syntax error because I looked up other similar programs and it is quite similar. How to deal with this issue?
screenshot: https://imgur.com/a/G4veic7
Posts: 12,031
Threads: 485
Joined: Sep 2016
show code here, we do not follow unknown links
Posts: 7
Threads: 2
Joined: Jul 2018
Jul-22-2018, 11:28 PM
(This post was last modified: Jul-22-2018, 11:29 PM by Larz60+.)
(Jul-22-2018, 11:18 PM)Larz60+ Wrote: show code here, we do not follow unknown links
def add(x,y):
return x+y
def multiply(x,y):
return x*y
print('Select operation')
print('1.Add')
print('2.Multiply')
choice= input('Enter choice (1/2): ')
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
elif choice =='2':
print(num1,"*",num2,"=", multiply(num1,num2))
else:
print'Invalid' this is the code.
Posts: 12,031
Threads: 485
Joined: Sep 2016
Error: File "/run/media/Larz60p/Data-4TB/python/e-h/f/forum/a-f/calc/src/junk1.py", line 23
print'Invalid'
^
SyntaxError: invalid syntax
This is why it is important to provide all information necessary to determine what is wrong.
You have a syntax error, the script never ran.
change line 23
# From:
print'Invalid'
# To:
print('Invalid') make sure your indentation is 4 spaces. That's important
Posts: 7
Threads: 2
Joined: Jul 2018
(Jul-22-2018, 11:35 PM)Larz60+ Wrote: Error: File "/run/media/Larz60p/Data-4TB/python/e-h/f/forum/a-f/calc/src/junk1.py", line 23
print'Invalid'
^
SyntaxError: invalid syntax
This is why it is important to provide all information necessary to determine what is wrong.
You have a syntax error, the script never ran.
change line 23
# From:
print'Invalid'
# To:
print('Invalid') make sure your indentation is 4 spaces. That's important I'm not getting an error, when it tells me to put in two numbers it goes instantly to the else line and prints 'invalid'. It does not perform the calculation.
Posts: 4,220
Threads: 97
Joined: Sep 2016
Larz60+ ran that code in Python 3.x, that's why he got that error. You're running that code in Python 2.x, that's why you're getting your error. In Python 2.x, input evaluates the user's input before returning it, so it is returning an integer to choice. That does not equal either of the strings you are testing against. Either use raw_input, or test against integer values.
Posts: 7
Threads: 2
Joined: Jul 2018
Jul-23-2018, 01:13 AM
(This post was last modified: Jul-23-2018, 01:13 AM by shakeelthomas.)
Wow i changed input to raw_input and it worked.
@above: how am i using python 2x
Posts: 4,220
Threads: 97
Joined: Sep 2016
(Jul-23-2018, 01:13 AM)shakeelthomas Wrote: how am i using python 2x
It's what's installed on your machine. You appear to be using Windows, so it doesn't come with the system. So you must have installed Python 2.x. If you go to the command line and type python, it should tell you exactly what version you are running.
How/what did you install?
Posts: 7
Threads: 2
Joined: Jul 2018
Jul-23-2018, 03:45 AM
(This post was last modified: Jul-23-2018, 03:45 AM by shakeelthomas.)
(Jul-23-2018, 03:17 AM)ichabod801 Wrote: (Jul-23-2018, 01:13 AM)shakeelthomas Wrote: how am i using python 2x
It's what's installed on your machine. You appear to be using Windows, so it doesn't come with the system. So you must have installed Python 2.x. If you go to the command line and type python, it should tell you exactly what version you are running.
How/what did you install? I use netbeans IDE and installed the python plugin via the website. I didn't actually download python from the website. I don't know how to check the version with this IDE.
Is there anyway i can get python 3x with netbeans? I'm already losing motivation to learn python.
Posts: 12,031
Threads: 485
Joined: Sep 2016
Jul-23-2018, 03:53 AM
(This post was last modified: Jul-23-2018, 03:53 AM by Larz60+.)
You're mixing apples with oranges.
Net beans is Java
you can test python from command line with:
python -V May I suggest that you take a look at snippsat's python install tutorials
for windows:
Part1
Part2
or for Linux:
Part1
Part2
|