Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
simple calculator
#1
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
Reply
#2
show code here, we do not follow unknown links
Reply
#3
(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.
Reply
#4
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
Reply
#5
(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.
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
Wow i changed input to raw_input and it worked.

@above: how am i using python 2x
Reply
#8
(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?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
(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.
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple calculator ryza 3 1,152 Sep-21-2022, 04:42 PM
Last Post: deanhystad
  Simple Distance Between Points Calculator Spade 1 1,755 Sep-17-2021, 03:14 PM
Last Post: DeaD_EyE
  Printing simple calculator answer LCFC2020 2 2,193 Dec-05-2020, 02:07 PM
Last Post: MK_CodingSpace

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020