How to add two numbers?

This is the place for queries that don't fit in any of the other categories.

How to add two numbers?

Postby stranac » Thu Feb 07, 2013 8:02 pm

Figured we needed at least one coding post.

So how do I get a result of adding a and b?
My code gives me "SyntaxError: can't assign to operator":
Code: Select all
a = 2
b = 2
a + b = c
print c
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1244
Joined: Thu Feb 07, 2013 3:42 pm

Re: How to add two numbers?

Postby Yoriz » Fri Feb 08, 2013 1:49 am

Hey :D

You see how you wanted the variable a to contain the value of 2, so you have the a first followed by = (which means assign) and then the 2,
to assign the value of a + b to c, the c needs to go first and then assign it using = to the result of adding a + b.
The altered code is shown below.

Code: Select all
a = 2
b = 2
c = a + b
print c
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1158
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: How to add two numbers?

Postby KevinD » Fri Feb 08, 2013 3:20 am

That was really helpful - thanks!

How do I get Python to display "Hello, World" on a blank screen?
Quanto lignum posset materiae materietur marmota Chuck si materiam possit materiari foedans, penitus lignum?
KevinD
 
Posts: 30
Joined: Fri Feb 08, 2013 3:15 am

Re: How to add two numbers?

Postby stranac » Fri Feb 08, 2013 12:02 pm

Thanks, Yoriz!
That worked.

KevinD:

Even I know that one.
You use print for that, like my 'print c'.

This should work:
Code: Select all
print '"Hello, World"'
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1244
Joined: Thu Feb 07, 2013 3:42 pm

Re: How to add two numbers?

Postby Mekire » Fri Feb 08, 2013 12:08 pm

When I run this code:
Code: Select all
print '"Hello, World"'
I'm getting this weird problem:
Code: Select all
SyntaxError: invalid syntax

What the heck am I doing wrong?
User avatar
Mekire
 
Posts: 1121
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: How to add two numbers?

Postby Yoriz » Fri Feb 08, 2013 1:25 pm

Mekire wrote:When I run this code:
Code: Select all
print '"Hello, World"'
I'm getting this weird problem:
Code: Select all
SyntaxError: invalid syntax

What the heck am I doing wrong?

I'm guessing you are using python 3, print changed to a function so you need to use print like this.
Code: Select all
print('"Hello, World"')

See this link for more info http://docs.python.org/3.0/whatsnew/3.0.html#print-is-a-function
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1158
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: How to add two numbers?

Postby KevinD » Fri Feb 08, 2013 3:58 pm

Can we think of any more pre-beginner questions to post?
Quanto lignum posset materiae materietur marmota Chuck si materiam possit materiari foedans, penitus lignum?
KevinD
 
Posts: 30
Joined: Fri Feb 08, 2013 3:15 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests