This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Learn Python the Hard Way

My recent threads

You haven't posted any discussions yet.

Recently updated threads

Exercise 5

Go back to: General discussion

I just finished exercise 5 so I'll put up my input and output below
This is without extra credit
--- INPUT ----

# Assign variables to the attributes of the writer
my_name = 'Zed A. Shaw'
my_age = 35
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'
 
# print the statements describing the writer
# %d is used in replace of an integer and %s is in place of strings
print "Let's talk about %s." % my_name
print "He's %d inches tall." % my_height
print "He's %d pounds heavy." % my_weight
print "Actually that's not too heavy."
print "He's got %s eyes and %s hair." % (my_eyes, my_hair)
print "His teeth are usualy %s depending on the coffee." % my_teeth
 
# this line looks a bit complicated but you can understand it if you aply the concepts learned in other chapeters

--- Output ---

Let's talk about Zed A. Shaw.
He's 74 inches tall.
He's 180 pounds heavy.
Actually that's not too heavy.
He's got Blue eyes and Brown hair.
His teeth are usualy White depending on the coffee.
If I add 35, 74, and 180 I get 289.

EXTRA CREDIT
The first part was simple it didn't do anything apart from change the look of .
Part 2 was a little harder and a found out that it preformed repr making it an object
Part 3 I found a good website here http://docs.python.org/release/2.5.2/lib/typesseq-strings.html
Part 4 was fun and I got a bit carried away and made a set of functions for it.
At first I wrote this:


def pounds_to_kilos(pounds):
    kilos = pounds /2.20462262
    return kilos
 
def kilos_to_pounds(kilos):
    pounds = kilos /0.45359237
    return pounds
 
def inches_to_centimeters(inches):
    centimeters = inches /2.54
    return centimeters
 
def inches_to_centimeters(inches):
    centimeters = inches /2.54
    return centimeters
 
def centimeters_to_inches(centimeters):
    inches = centimeters /0.393700787
    return centimeters

and then I added 4 other functions changing meters to centimeters, feet to inches, etc.:

def centimeters_to_meters(centimeters):
    meters = centimeters /100
    return meters
 
def meters_to_centimeters(meters):
    centimeters = meters * 100
    return centimeters
 
def inches_to_feet(inches):
    feet = inches /12
    return feet
 
def feet_to_inches(feet):
    inches = feet *12
    return inches

This was a good exercise.

 

Trent Branson's picture
Trent Branson
Mon, 2011-01-24 03:22

Sorry about the typo's.

Seth Moskowitz's picture
Seth Moskowitz
Mon, 2011-01-24 18:47

Whoa! What you did is well above my skill level.

Steve Phelps's picture
Steve Phelps
Tue, 2011-01-25 11:24

I am in Seth's boat :) but how you defined the functions makes sense. So this convert yards to inches:

def yards_to_inches(yards):
inches = yards * 36
return inches

Red Olaf's picture
Red Olaf
Wed, 2011-01-26 23:33

Same for me. I just defined some variables:

centimeters = height * 2.54
kilos = weight / 2.20462262

The output with the other variables from the exercise:

Let's talk about 'Zed A. Shaw'.
He's 74 inches tall.
That's 187 in centimeters.
He's 180 pounds heavy.
Actually that's not too heavy.
That's only 81 in kilograms.
He's got Blue eyes and Brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.

michael chang's picture
michael chang
Mon, 2011-01-31 23:03

I did the variable thing, too.

How do you use those functions? Do you print within the script or call them from Terminal?

jennifer karmon's picture
jennifer karmon
Tue, 2011-02-01 06:38

I have a profoundly dunderheaded question for you, Michael (and sorry, I am unhelpful and have no answer to the question you posed). What does the word "script" mean in a programming context, as you use it above? Is that the program itself here? I have heard the word "script" used a lot (as in "scripting language," for example), but I'm totally unclear on what it means.

ZM L's picture
ZM L
Tue, 2011-02-01 08:40

I'm not Michael but from my understanding, programmers have different ways of calling things. A function (the one you define with "def") could also be called a method or a sub routine or whatever. Some programmers might even want to say that a function is a method that provides a return value.etc

So for script it can probably mean a small program file that is run on the command line, it might also mean a small snippet of code that does something useful.etc

But most importantly, what you call them doesn't matter really much actually for most people. Just need to know the different names so you can communicate well with other programmers when they use such lingos.

michael chang's picture
michael chang
Tue, 2011-02-01 11:41

Hi Jennifer, I don't think it's dumb. I have no idea what some of the words mean. What's a scalar?? the way I used "script" meant all the text we typed up in our ex5.py file. I couldn't (and still don't) understand how the functions to using the "def syntax" are called (or summoned!). Are they typed within the script or within the Terminal?

ZM L's picture
ZM L
Wed, 2011-02-02 05:15

Hi, from my understanding functions/methods/subroutines are just block of codes, ready to be executed when called upon.

So basically they just seat down there doing nothing until somebody say calculate_strength() for example. then all the code in calculate_strength() runs.

Functions are invented to help programmer save time and improve maintainability. After all it would be tedious to keep type out the same 10--12 lines of code each time you need to do that very same thing! So programmers built a function and keep the common code there. And when they need it, they just call the function and it runs! And they could use this function at many other places as well.

Lastly, when they need to change how that piece of code work, they only got 1 place to change. than to go around changing the code all over the place.

Here an example:

def greetings():
print "Hello"

print "My name is Jack"
greetings()

print "I'm Jane"
greetings()

where greetings() will print "Hello". However let's say if you want to change "Hello" to Hi" all you need to do is go to the greetings function and change

print "Hello" to print "Hi" at 1 place. And everything changes as well.

Hope that helps.

michael chang's picture
michael chang
Wed, 2011-02-02 22:47

hello ZM L,
thank you for the detailed response. :)

Robert Dragan's picture
Robert Dragan
Fri, 2011-02-11 14:47

I think you have a few logical errors in your code.

In inches_to_centimeters(inches) you should have
centimeters = inches * 2.54
, instead of
centimeters = inches /2.54

Also, in centimeters_to_inches(centimeters) you should have
inches = centimeters * 0.393700787
or
inches = centimeters / 2.54
instead of
inches = centimeters / 0.393700787
Plus you should return inches, not centimeters.

ZM L's picture
ZM L
Mon, 2011-01-24 10:54

just did exercise 5, question 2 for extra credits puzzles me, it says %r prints "this no matter what", but I could easily use a %s and a number or a piece of text is also printed as well.

It seem many of these formatting characters could be used for each other, such as I could place an integer for %d as well as %i and %s and %r?

This is getting me confused. Why are there so many formatting characters when they are about the same? Is it for backward compatibility?

Alexis Tsonis's picture
Alexis Tsonis
Mon, 2011-01-24 15:40

%r converts everything into a string,using the repr() function
You can check repr() here: http://docs.python.org/library/functions.html#repr
%d is equal to %i,while %s is only for strings

ZM L's picture
ZM L
Tue, 2011-01-25 07:51

Hmm I start to notice some recurring decimals starting to print differently in normal %s and repr "%r".

M. Volz's picture
M. Volz
Wed, 2011-02-02 18:13

There are situations where you might need to have a general formatting character. For instance, suppose you have a program which takes in user input, and you don't know whether the user will be giving you a number or a string. If you use %r, then it will work no matter what and not throw an error.

In the reverse case, there are some instances when you want the program to throw an error if the wrong thing is entered. For instance, if you want them to give you a number, but they type in a string, you can "catch" the error and print an error message instead. ("Catching" errors is covered in Exercise 48, Advanced User Input.)

However, I think that %i and %d are actually identical; someone please correct me if I'm wrong. Sometimes languages are redundant for user friendliness or, like you pointed out, backwards compatibility. Why does HTML have both an <em> and an <i> tag?

Arbi Derhartunian's picture
Arbi Derhartunian
Thu, 2011-01-27 00:59

How come we are getting the solutions before we attempt the problem

M. Volz's picture
M. Volz
Thu, 2011-01-27 03:49

Some people are posting the answers they got to compare them with other people's.

M. Volz's picture
M. Volz
Wed, 2011-02-02 17:45

For anyone who is confused by the parent, it's because it uses functions, which we don't get to until Exercise 18! There are a number of ways to complete this exercise that don't involve using functions so don't feel like you need to jump ahead if you don't feel comfortable doing so.

Dorene DeMars's picture
Dorene DeMars
Sun, 2011-02-06 20:19

Here's my program, exactly as I typed it the first time. It worked the way it was supposed to. I was a little worried about the last two lines...why didn't we just keep going on Line 17? Did we need Line 18 or was that just visual preference?

my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'

print "Let's talk about %s." % my_name
print "He's %d inches tall." % my_height
print "He's %d pounds heavy." % my_weight
print "Actually that's not too heavy."
print "He's got %s eyes and %s hair." % (my_eyes, my_hair)
print "His teeth are usually %s depending on the coffee." % my_teeth

# this line is tricky, try to get it exactly right
print "If I add %d, %d, and %d I get %d." % (
my_age, my_height, my_weight, my_age + my_height + my_weight)