Print statement not printing as it suppose to

S

Sam

hi everybody i am just starting to learn python, i was writing a simple i/o program but my print statement is acting weird. here is my code i want to know why it prints this way. thank you


car=int(input("Lamborghini tune-up:"))

rent=int(input('\nManhatan apartment: '))

gifts=int(input('\nRandom Gifts: '))

total=car+rent+gifts

print("\nThe total amount required is ", total )


OUTPUT

Lamborghini tune-up:1000

Manhatan apartment: 2300

Random Gifts: 234
('\nThe total amount required is ', 3534)



===> the problem is obviously on the last print statement that is supposed to print the outut
 
T

Tim Delaney

hi everybody i am just starting to learn python, i was writing a simple
i/o program but my print statement is acting weird. here is my code i want
to know why it prints this way. thank you

print("\nThe total amount required is ", total )


OUTPUT

('\nThe total amount required is ', 3534)

===> the problem is obviously on the last print statement that is supposed
to print the outut

Check your version of Python. The output you have given says that you're
using a Python 2 version, but the print syntax you're using is for Python
3. Unfortunately, you've hit one of the edge cases where they produce
different output.

As a general rule, either use % formatting or format()to produce a single
string to print, rather than relying on print to output them correctly for
you (or using string concatenation). Since you're only just starting you
won't have got to them yet - the simplest way to to it is to just insert
the string representation of all parameters. The above done using %
formatting would be:

print("\nThe total amount required is %s" % (total,))

which will produce the same output on both Python 2 and Python 3. Note the
double space before %s - that matches your print statement (there would be
soft-space inserted in your print statement, which is another reason not to
rely on print for anything other than single strings). If you didn't want
that extra space, it's easy to delete.

Tim Delaney
 
E

Emiliano Carlos de Moraes Firmino

Remove both brackets in last line, You are creating a tuple in last
statement not making a function call.
 
J

John Gordon

In said:
print("\nThe total amount required is ", total )

('\nThe total amount required is ', 3534)

In older versions of python (like the one you are using), 'print' is a
statement instead of a function.

In other words, it is used like this:

name = "Bob"
print "Hello ", name

Because there are parentheses around the text to be printed, your version
of python is interpreting it as a tuple. Remove the parentheses and you
should be ok.
 
C

Chris Angelico

car=int(input("Lamborghini tune-up:"))
print("\nThe total amount required is ", total )
OUTPUT
('\nThe total amount required is ', 3534)

As others have said, this output indicates that you're running under a
Python 2.x interpreter. I strongly recommend you switch to running
under Python 3.x - do not take the simple advice that might make it
work in both, because you have other differences which will trip you
up. In Python 2, the input function is extremely dangerous and should
be avoided: it *evaluates* its argument. (If you really *want* to
evaluate something, you can call the eval() function explicitly. You
don't want it to be hidden behind the innocuously-named input().)
Download Python 3.3 (or later) and start using that; you'll find it's
by far the better interpreter - years of improvements on top of the
version you're using there.

ChrisA
 
D

Dave Angel

print("\nThe total amount required is ", total )


('\nThe total amount required is ', 3534)

===> the problem is obviously on the last print statement that is supposed to print the outut

Others have pointed out the version discrepancy. But I'll also ask what
you're learning Python from. If you've got a tutorial or book that uses
Python 3.x, then forget 2.x and install 3.3

If you're learning it in a class, then the instructor should already
have told you what version to use.

It is possible to install both 2.7 and 3.3, but you probably don't need
the confusion while you're learning.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top