Learn Python the Hardway exercise 11 question 4

J

Joseph Sanoyo

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?
 
S

Steven D'Aprano

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?

I'm sorry, are you asking a question and expecting an answer, replying to
somebody else's question, or just sharing something you thought was
interesting?
 
W

Web Dreamer

Joseph Sanoyo a écrit ce jeudi 31 mars 2011 05:03 dans <b2293547-6153-48e6-
(e-mail address removed)> :
File "<stdin>", line 1
print "How old are you?", age = raw_input()
^
SyntaxError: invalid syntax
File "<stdin>", line 1
print "How tall are you?", height = raw_input()
^
SyntaxError: invalid syntax
File "<stdin>", line 1
print "How much do you weigh?", weight = raw_input()
^
SyntaxError: invalid syntax
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.
 
C

Chris Angelico

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!
 
C

Chris Angelico


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
 
G

geremy condra

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!

I know it's tongue-in-cheek, but please, please, please don't do this.

Geremy Condra
 
C

Chris Angelico

I know it's tongue-in-cheek, but please, please, please don't do this.

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

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top