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 6

Go back to: General discussion

Here's my source code and output with part 1 and 2 of Extra credit included:

--- Source Code ---

# x assigned to a string replacing %d with 10
x = "There are %d types of people." % 10
# binary assigned to the str "binary" for later use
binary = "binary"
# the same with do_not 
do_not = "don't"
# replace both %s with the variables binary and do_not
# 2 strings inside a string were the %s is
y = "Those who know %s and those who %s." % (binary, do_not)
 
# print the variable x
print x
# same with y
print y
 
# print I said and then what you said
# 1 string inside a string
print "I said : %r." % x
# print I also said and the other line you said
# 1 string inside a string
print "I also said: '%s'." % y
 
# assign hilarious to false
hilarious = False
# assign joke_evaluation to Isn't that joke funny? and leave the value of %r as none
joke_evaluation = "Isn't that joke so funny?! %r"
 
# print joke_eavluation using hilarious as %r
print joke_evaluation % hilarious
 
# assign w to the string
w = "This is the left side of..."
# assign e to another string
e = "a string with a right side."
 
# print both w and e
print w + e

--- Output --- 

There are 10 types of people.
Those who know binary and those who don't.
I said : 'There are 10 types of people.'.
I also said: 'Those who know binary and those who don't.'.
Isn't that joke so funny?! False
This is the left side of...a string with a right side.

EXTRA CREDIT
Parts 1 and 2 I've done in the source code
part 3 is false because when we assigned hilarious to false we didn't use quotes because it's a value not a string
part 4 : in python if you add string you simply put them together so 'i'+'i' would be 'ii'
BTW try multiplying strings
 
ZM L's picture
ZM L
Mon, 2011-01-24 11:15

hmm so we can add strings together and multiply strings. But we can't divide string it seem. "aaaa"/4 didn't give me an a :(.

nada A.S's picture
nada A.S
Mon, 2011-01-24 12:27

In python we can only use (+,*) with strings , because that operations are arithmetic means deal just with numbers .

The facility in python by using (+) helps us to combine strings together ;for instance the output of "1"+"2" same as '1'+'2' is 12.

Also using (*) operation is used to repeat string number of times for example: print "Hello "*3, will produce
Hello Hello Hello.

Hope that was helpful!

Dorene DeMars's picture
Dorene DeMars
Wed, 2011-02-09 07:53

Here's my program...it generated the desired output so I am pleased about that. I think, however, I am the butt of the binary joke there. Because I don't quite get it! :-)

x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary, do_not)

print x
print y

print "I said: %r." % x
print "I also said: '%s'." % y

hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"

print joke_evaluation % hilarious

w = "This is the left side of..."
e = "a string with a right side."

print w + e

Amene Katanda's picture
Amene Katanda
Wed, 2011-02-09 16:36

@Dorene on the binary joke: (there are 10 types of people: those who understand binary and those who don't). Remember in binary the number 2 is represented with 10. Got it now?

Guntis L's picture
Guntis L
Wed, 2011-02-09 16:41

So you are the other type :) Here's a good material on how to count in binary - http://redd.it/9oabt