Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54513 > unrolled thread

Print statement not printing as it suppose to

Started bySam <anasdahany@gmail.com>
First post2013-09-20 14:57 -0700
Last post2013-09-21 04:40 +0000
Articles 6 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Print statement not printing as it suppose to Sam <anasdahany@gmail.com> - 2013-09-20 14:57 -0700
    Re: Print statement not printing as it suppose to Tim Delaney <timothy.c.delaney@gmail.com> - 2013-09-21 08:11 +1000
    Re: Print statement not printing as it suppose to Emiliano Carlos de Moraes Firmino <emiliano.firmino@gmail.com> - 2013-09-20 18:11 -0400
    Re: Print statement not printing as it suppose to John Gordon <gordon@panix.com> - 2013-09-20 22:40 +0000
    Re: Print statement not printing as it suppose to Chris Angelico <rosuav@gmail.com> - 2013-09-21 12:50 +1000
    Re: Print statement not printing as it suppose to Dave Angel <davea@davea.name> - 2013-09-21 04:40 +0000

#54513 — Print statement not printing as it suppose to

FromSam <anasdahany@gmail.com>
Date2013-09-20 14:57 -0700
SubjectPrint statement not printing as it suppose to
Message-ID<05bbf1a3-6480-48ee-8984-2482b90c79c0@googlegroups.com>
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

[toc] | [next] | [standalone]


#54514

FromTim Delaney <timothy.c.delaney@gmail.com>
Date2013-09-21 08:11 +1000
Message-ID<mailman.202.1379715089.18130.python-list@python.org>
In reply to#54513

[Multipart message — attachments visible in raw view] — view raw

On 21 September 2013 07:57, Sam <anasdahany@gmail.com> wrote:

> 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

[toc] | [prev] | [next] | [standalone]


#54515

FromEmiliano Carlos de Moraes Firmino <emiliano.firmino@gmail.com>
Date2013-09-20 18:11 -0400
Message-ID<mailman.203.1379715584.18130.python-list@python.org>
In reply to#54513

[Multipart message — attachments visible in raw view] — view raw

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


2013/9/20 Sam <anasdahany@gmail.com>

> 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
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Emiliano Carlos de Moraes Firmino
Desenvolvedor | PMT | INdT
Universitário | Engenharia da Computação | UEA
emiliano.firmino@gmail.com | (92) 9196-3922

[toc] | [prev] | [next] | [standalone]


#54517

FromJohn Gordon <gordon@panix.com>
Date2013-09-20 22:40 +0000
Message-ID<l1iis0$itm$1@reader1.panix.com>
In reply to#54513
In <05bbf1a3-6480-48ee-8984-2482b90c79c0@googlegroups.com> Sam <anasdahany@gmail.com> writes:

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

> OUTPUT

> ('\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.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#54527

FromChris Angelico <rosuav@gmail.com>
Date2013-09-21 12:50 +1000
Message-ID<mailman.212.1379731840.18130.python-list@python.org>
In reply to#54513
On Sat, Sep 21, 2013 at 7:57 AM, Sam <anasdahany@gmail.com> wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#54531

FromDave Angel <davea@davea.name>
Date2013-09-21 04:40 +0000
Message-ID<mailman.216.1379738432.18130.python-list@python.org>
In reply to#54513
On 20/9/2013 17:57, Sam wrote:

>
> 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.

-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web