Jump to content

What is the problem in this python code

TheGrim123321

I'm very new to python so I figured I'd try to write a simple calculator that only does addition (I'm new give me a break)

 

Code

number_1 = input('Enter a number: ')
number_2 = input('Enter a second number: ')
addition = 1
function = func (input('1. Addition'))
if func = 1
    print "number_1 + number_2"

 

Error

 

 if func = 1
            ^
SyntaxError: invalid syntax

Process finished with exit code 1

 

Plz help

 

TheGrim123321

CPU: I3-4170Cooler: Hyper TX3Mobo:Biostar B85MGMemory: G.Skill Ripjaw 2x4GBStorage: Barracuda 500GBGPU: Zotac GTX660Case: Fractal 1100PSU: Evga 500WMonitors: FHX2153L 21.5"│V193WEJb 19"Keyboard/Mouse: CMStorm DevastorAudio: MonoPrice 8323Microphone: BlueYeti BlackoutOS: MSX(Win10) Quote me or @TheGrim123321 to get my attention.
Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, SSL said:

Equality operator is not a single "="

I tried 2 == same error

 

I"m using pycharm

TheGrim123321

CPU: I3-4170Cooler: Hyper TX3Mobo:Biostar B85MGMemory: G.Skill Ripjaw 2x4GBStorage: Barracuda 500GBGPU: Zotac GTX660Case: Fractal 1100PSU: Evga 500WMonitors: FHX2153L 21.5"│V193WEJb 19"Keyboard/Mouse: CMStorm DevastorAudio: MonoPrice 8323Microphone: BlueYeti BlackoutOS: MSX(Win10) Quote me or @TheGrim123321 to get my attention.
Link to comment
Share on other sites

Link to post
Share on other sites

And it should be :

if func == 1 :
	print <etc>

With ":"

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, TheGrim123321 said:

I tried 2 == same error

 

I"m using pycharm

 

func doesn't look like a valid identifier. Should it be function?

Link to comment
Share on other sites

Link to post
Share on other sites

As of right now I'm

number_1 = input('Enter a number: ')
number_2 = input('Enter a second number: ')
additon = 1
function = (input('1. Addition'))
if input == 1 :
   print ("number_1 + number_2")

however it's not printing. I'm not getting an error code tho

TheGrim123321

CPU: I3-4170Cooler: Hyper TX3Mobo:Biostar B85MGMemory: G.Skill Ripjaw 2x4GBStorage: Barracuda 500GBGPU: Zotac GTX660Case: Fractal 1100PSU: Evga 500WMonitors: FHX2153L 21.5"│V193WEJb 19"Keyboard/Mouse: CMStorm DevastorAudio: MonoPrice 8323Microphone: BlueYeti BlackoutOS: MSX(Win10) Quote me or @TheGrim123321 to get my attention.
Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, TheGrim123321 said:

As of right now I'm


number_1 = input('Enter a number: ')
number_2 = input('Enter a second number: ')
additon = 1
function = (input('1. Addition'))
if input == 1 :
   print ("number_1 + number_2")

however it's not printing. I'm not getting an error code tho

 

input is a reserved word. You can't use it as a variable name.

Link to comment
Share on other sites

Link to post
Share on other sites

So what's input in this case? You never define input as 1 do you? Also (I think) input() gets the user input as a string so you may have to convert them to ints first.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, ElfFriend said:

So what's input in this case? You never define input as 1 do you? Also (I think) input() gets the user input as a string so you may have to convert them to ints first.

How do I do that

I'm extremely knew to python and programming

TheGrim123321

CPU: I3-4170Cooler: Hyper TX3Mobo:Biostar B85MGMemory: G.Skill Ripjaw 2x4GBStorage: Barracuda 500GBGPU: Zotac GTX660Case: Fractal 1100PSU: Evga 500WMonitors: FHX2153L 21.5"│V193WEJb 19"Keyboard/Mouse: CMStorm DevastorAudio: MonoPrice 8323Microphone: BlueYeti BlackoutOS: MSX(Win10) Quote me or @TheGrim123321 to get my attention.
Link to comment
Share on other sites

Link to post
Share on other sites

Overall, it might be easier if you can give us the pseudo-code of what you are trying to accomplish, and how. As it is I'm kind of confused what you're trying to do.

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, SSL said:

 

input is a reserved word. You can't use it as a variable name.

That's what I was trying to use func as a var

TheGrim123321

CPU: I3-4170Cooler: Hyper TX3Mobo:Biostar B85MGMemory: G.Skill Ripjaw 2x4GBStorage: Barracuda 500GBGPU: Zotac GTX660Case: Fractal 1100PSU: Evga 500WMonitors: FHX2153L 21.5"│V193WEJb 19"Keyboard/Mouse: CMStorm DevastorAudio: MonoPrice 8323Microphone: BlueYeti BlackoutOS: MSX(Win10) Quote me or @TheGrim123321 to get my attention.
Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, TheGrim123321 said:

That's what I was trying to use func as a var

 

But you weren't assigning "func" anywhere.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, TheGrim123321 said:

How do I do that

I'm extremely knew to python and programming

Converting from a string to an int can be done with newVaraible=int(oldVariable).

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, TheGrim123321 said:

As of right now I'm


number_1 = input('Enter a number: ')
number_2 = input('Enter a second number: ')
additon = 1
function = (input('1. Addition'))
if input == 1 :
   print ("number_1 + number_2")

however it's not printing. I'm not getting an error code tho

inputs are taken in as strings, so even if you type in "1", it'll be interpreted as the character "1", not the integer value of 1, thus "1" == 1 does not satisfy the equality, and the print statement is skipped. You need to either convert the input to an int, or compare it with the character "1" instead.

 

if int(input) == 1:
    print()

[or]

if input == '1':     #note the quotations around '1'
    print()

You will also need to convert number_1 and number_2 to ints in the same manner (int(input()) if you want to add them together, otherwise since they're strings right now, the addition operator will just combine them (i.e. if number_1 is set to "4" and number_2 is set to "8", they'll "add up" to 48).

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Glenwing said:

inputs are taken in as strings, so even if you type in "1", it'll be interpreted as the character "1", not the integer value of 1, thus "1" == 1 does not satisfy the equality, and the print statement is skipped. You need to either convert the input to an int, or compare it with the character "1" instead.

 


if int(input) == 1:
    print()

[or]

if input == '1':     #note the quotations around '1'
    print()

You will also need to convert number_1 and number_2 to ints in the same manner (int(input()) if you want to add them together, otherwise since they're strings right now, the addition operator will just combine them (i.e. if number_1 is set to "4" and number_2 is set to "8", they'll "add up" to 48).

I generally rather test that the input can indeed be an int first since I've lost all trust in users at this point :P

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, SSL said:

Overall, it might be easier if you can give us the pseudo-code of what you are trying to accomplish, and how. As it is I'm kind of confused what you're trying to do.

number_1 = input('Enter a number: ')
number_2 = input('Enter a second number: ')
additon = 1
function = (input('1. Addition'))
if input == 1 :
   print ("number_1 + number_2")

 

Pseudo code

number_ 1 = user input

number_2 = user input

addition = 1

math = user input

if userinput = 1 then print number_1 + number_2

TheGrim123321

CPU: I3-4170Cooler: Hyper TX3Mobo:Biostar B85MGMemory: G.Skill Ripjaw 2x4GBStorage: Barracuda 500GBGPU: Zotac GTX660Case: Fractal 1100PSU: Evga 500WMonitors: FHX2153L 21.5"│V193WEJb 19"Keyboard/Mouse: CMStorm DevastorAudio: MonoPrice 8323Microphone: BlueYeti BlackoutOS: MSX(Win10) Quote me or @TheGrim123321 to get my attention.
Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, TheGrim123321 said:

number_1 = input('Enter a number: ')
number_2 = input('Enter a second number: ')
additon = 1
function = (input('1. Addition'))
if input == 1 :
   print ("number_1 + number_2")

 

Pseudo code

number_ 1 = user input

number_2 = user input

addition = 1

math = user input

if userinput = 1 then print number_1 + number_2

 

What is the "addition" variable for?

Link to comment
Share on other sites

Link to post
Share on other sites

okay so what's the point of addition = 1? You don't seem to use it for anything? Same with math

Link to comment
Share on other sites

Link to post
Share on other sites

Also, you can't do string interpolation like you are doing with "number_1 + number_2". That will literally print "number_1 + number_2".

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, ElfFriend said:

okay so what's the point of addition = 1? You don't seem to use it for anything? Same with math

 

2 minutes ago, SSL said:

 

What is the "addition" variable for?

So when the user types in 1 it knows its the addition variable should it be

if input == addition

 

should I turn

function = (input('1. Addition'))

 

into

 

input('1. Addition')

TheGrim123321

CPU: I3-4170Cooler: Hyper TX3Mobo:Biostar B85MGMemory: G.Skill Ripjaw 2x4GBStorage: Barracuda 500GBGPU: Zotac GTX660Case: Fractal 1100PSU: Evga 500WMonitors: FHX2153L 21.5"│V193WEJb 19"Keyboard/Mouse: CMStorm DevastorAudio: MonoPrice 8323Microphone: BlueYeti BlackoutOS: MSX(Win10) Quote me or @TheGrim123321 to get my attention.
Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, SSL said:

Also, you can't do string interpolation like you are doing with "number_1 + number_2". That will literally print "number_1 + number_2".

If I remove the " " will it print it right

TheGrim123321

CPU: I3-4170Cooler: Hyper TX3Mobo:Biostar B85MGMemory: G.Skill Ripjaw 2x4GBStorage: Barracuda 500GBGPU: Zotac GTX660Case: Fractal 1100PSU: Evga 500WMonitors: FHX2153L 21.5"│V193WEJb 19"Keyboard/Mouse: CMStorm DevastorAudio: MonoPrice 8323Microphone: BlueYeti BlackoutOS: MSX(Win10) Quote me or @TheGrim123321 to get my attention.
Link to comment
Share on other sites

Link to post
Share on other sites

Here's what I'd do:

number_1 = int(input('Enter a number: '))
number_2 = int(input('Enter a second number: '))
function = int(input('1. Addition'))
if function == 1 :
   print (number_1+number_2)

 

Edit: Actually I'd do it slightly cleaner (nicer prompts) but this should work...

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, TheGrim123321 said:

If I remove the " " will it print it right

 

Yes.

 

4 minutes ago, TheGrim123321 said:

 

So when the user types in 1 it knows its the addition variable should it be


if input == addition

 

should I turn


function = (input('1. Addition'))

 

into

 

input('1. Addition')

 

I think you should write out what you want to happen in plain English (or whatever language you prefer). Then translate that to Python. I could just tell you what to code, but I don't think will help you learn.

 

I will suggest that "operator" or "operation" would be a clearer variable name than "function".

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, ElfFriend said:

Here's what I'd do:


[code]
number_1 = int(input('Enter a number: '))
number_2 = int(input('Enter a second number: '))
function = int(input('1. Addition'))
if function == 1 :
   print (number_1+number_2)
[/code]

This works thank you

TheGrim123321

CPU: I3-4170Cooler: Hyper TX3Mobo:Biostar B85MGMemory: G.Skill Ripjaw 2x4GBStorage: Barracuda 500GBGPU: Zotac GTX660Case: Fractal 1100PSU: Evga 500WMonitors: FHX2153L 21.5"│V193WEJb 19"Keyboard/Mouse: CMStorm DevastorAudio: MonoPrice 8323Microphone: BlueYeti BlackoutOS: MSX(Win10) Quote me or @TheGrim123321 to get my attention.
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×