Learn Python the Hardway exercise 11 question 4

Discussion in 'Python' started by Joseph Sanoyo, Mar 31, 2011.

1. Joseph SanoyoGuest

print "How old are you?", age = raw_input()
print "How tall are you?", height = raw_input()
print "How much do you weigh?", weight = raw_input()
print "So, you're %r old, %r tall and %r heavy." % ( age, height,
weight)
Note:
Notice that we put a , (comma) at the end of each print line. This is
so that print doesn’t end the line with a newline and go to the next
line.
What You Should See
Extra Credit
1. Go online and find out what Python’s raw_input does.
\$ python ex11.py How old are you?
35 How tall are you?
6'2" How much do you weigh? 180lbs
So, you're '35' old, '6\'2"' tall and '180lbs' heavy.

Related to escape sequences, try to find out why the last line has
’6\’2"’ with that \’ sequence. See how the single-quote needs to be
escaped because otherwise it would end the string?

Joseph Sanoyo, Mar 31, 2011

2. Steven D'ApranoGuest

On Wed, 30 Mar 2011 20:03:09 -0700, Joseph Sanoyo wrote:

> print "How old are you?", age = raw_input() print "How tall are you?",
> height = raw_input() print "How much do you weigh?", weight =
> raw_input() print "So, you're %r old, %r tall and %r heavy." % ( age,
> height, weight)
> Note:
> Notice that we put a , (comma) at the end of each print line. This is so
> that print doesnâ€™t end the line with a newline and go to the next line.
> What You Should See
> Extra Credit
> 1. Go online and find out what Pythonâ€™s raw_input does. \$ python ex11.py
> How old are you?
> 35 How tall are you?
> 6'2" How much do you weigh? 180lbs
> So, you're '35' old, '6\'2"' tall and '180lbs' heavy.
>
> Related to escape sequences, try to find out why the last line has
> â€™6\â€™2"â€™ with that \â€™ sequence. See how the single-quote needs to be
> escaped because otherwise it would end the string?

somebody else's question, or just sharing something you thought was
interesting?

--
Steven

Steven D'Aprano, Mar 31, 2011

3. Web DreamerGuest

Joseph Sanoyo a écrit ce jeudi 31 mars 2011 05:03 dans <b2293547-6153-48e6-
> :

>>> print "How old are you?", age = raw_input()

File "<stdin>", line 1
print "How old are you?", age = raw_input()
^
SyntaxError: invalid syntax

>>> print "How tall are you?", height = raw_input()

File "<stdin>", line 1
print "How tall are you?", height = raw_input()
^
SyntaxError: invalid syntax

>>> print "How much do you weigh?", weight = raw_input()

File "<stdin>", line 1
print "How much do you weigh?", weight = raw_input()
^
SyntaxError: invalid syntax

>>> print "So, you're %r old, %r tall and %r heavy." % ( age, height,

weight)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'height' is not defined

Normal, since the above lines failed, age, height and weight are not defined

> What You Should See

The exact errors above.

> Extra Credit
> 1. Go online and find out what Python?s raw_input does.
> \$ python ex11.py How old are you?

File "ex11.py", line 1
print "How old are you?", age = raw_input()
^
SyntaxError: invalid syntax

> 35 How tall are you?
> 6'2" How much do you weigh? 180lbs
> So, you're '35' old, '6\'2"' tall and '180lbs' heavy.
>
> Related to escape sequences, try to find out why the last line has
> ?6\?2"? with that \? sequence. See how the single-quote needs to be
> escaped because otherwise it would end the string?

First all the above code needs to be fixed and you should write:
age = raw_input("How old are you? ")
height = raw_input("How tall are you? ")
weight = raw_input("How much do you weigh? " )
print "So, you're %r old, %r tall and %r heavy." % ( age, height, weight)

then:
\$ python ex11.py
How old are you? 10
How tall are you? 10
How much do you weigh? 10
So, you're '10' old, '10' tall and '10' heavy.

If you replace %r by %s in the print you get:
So, you're 10 old, 10 tall and 10 heavy.

--
Web Dreamer

Web Dreamer, Mar 31, 2011
4. Chris AngelicoGuest

On Fri, Apr 1, 2011 at 3:12 AM, eryksun () <> wrote:
> There appears to be a formatting error here.

So remind me again why Python likes whitespace to be significant?

</troll>

Chris Angelico
PS. Yes, I know "remind me again" is redundant. You have to make
mistakes when you troll, it's a moral imperative!

Chris Angelico, Mar 31, 2011
5. Chris AngelicoGuest

On Fri, Apr 1, 2011 at 4:54 AM, Dan Stromberg <> wrote:

I was trolling, I know the reasons behind it. Anyway, most people
don't share code by email! (Actually, since you seem to be the author
of that page - could you address that particular point? I think it's
probably as big an issue as any of the others, to today's coders -
"code semantics get destroyed by forums/email/etc/etc/etc".)

Solution: All emailed code should begin with
from __future__ import braces
And there you are, out of your difficulty at once!

Chris Angelico
tongue still firmly stuck in cheek

Chris Angelico, Mar 31, 2011
6. geremy condraGuest

On Thu, Mar 31, 2011 at 2:43 PM, eryksun () <> wrote:
> On Thursday, March 31, 2011 4:35:42 PM UTC-4, Chris Angelico wrote:
>>
>> I was trolling, I know the reasons behind it. Anyway, most people
>> don't share code by email! (Actually, since you seem to be the author
>> of that page - could you address that particular point? I think it's
>> probably as big an issue as any of the others, to today's coders -
>> "code semantics get destroyed by forums/email/etc/etc/etc".)
>>
>> Solution: All emailed code should begin with
>> from __future__ import braces
>> And there you are, out of your difficulty at once!

>
> You could paste it as a base64 stream, such as:
>
>
>> ZGVmIHNwYW0oKToNCiAgICBwcmludCAiU3BhbSEg
>> TG92ZWx5IHNwYW0hIExvdmVseSBzcGFtISI=

>
>
> Then decode and exec:
>
> In [1]: import base64
>
> In [2]: %cpaste
> Pasting code; enter '--' alone on the line to stop.
> :code="""> ZGVmIHNwYW0oKToNCiAgICBwcmludCAiU3BhbSEg
> :> TG92ZWx5IHNwYW0hIExvdmVseSBzcGFtISI="""
> :--
>
> In [3]: print base64.b64decode(code)
> def spam():
>    print "Spam! Lovely spam! Lovely spam!"
>
> In [4]: exec(base64.b64decode(code))
>
> In [5]: spam()
> Spam! Lovely spam! Lovely spam!

Geremy Condra

geremy condra, Mar 31, 2011
7. Chris AngelicoGuest

On Fri, Apr 1, 2011 at 8:57 AM, geremy condra <> wrote:

It would be more secure to base64 it and then rot13 the output.

Chris Angelico
/me is feeling evil today

=== Begin Base-Rotten 64-13 ===
FKDtq291oTDtLzHtoJ9lMFOmMJA1pzHtqT8tLzSmMGL0VTy0VTShMPO0nTIhVUWiqQRmVUEbMFOi
qKEjqKDhQDbAPxAbpzymVRShM2IfnJAiQDbioJHtnKZtMzIyoTyhMlOyqzyfVUEiMTS5QDb=
=== End Base-Rotten 64-13 ===

Chris Angelico, Mar 31, 2011
8. John BokmaGuest

Chris Angelico <> writes:

> On Fri, Apr 1, 2011 at 8:57 AM, geremy condra <> wrote:

>
> It would be more secure to base64 it and then rot13 the output.

Rot-13 twice, to make it even more secure ;-)

--
John Bokma j3b