Setting new values for variables in a string

Now you are going to generate variations on this note for two different students, with a dotted line between the two messages.

You want the output:

1
2
3
Hi Aditya. You achieved a score of 98. It's True that I enjoyed having you as a student.
--------------------------------------------------------------------------------
Hi Sue Troublemaker. You achieved a score of 46.

You write the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
name = 'Aditya'
score = 98
liked = True

message = "Hi %s. You achieved a score of %d." % (name, score)
extra = "It's %s that I enjoyed having you as a student." % liked

print message, extra
print "-" * 80

name = 'Sue Troublemaker'
score = 46
liked = False

print message

Does it give you the desired output?

No, below the dotted line it prints: Hi Aditya. You achieved a score of 98 in the class. No, the message for Sue Troublemaker starts on the same row as the dotted line Yes No, I get an error

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Hanif Robbani
Aug 2, 2014

m e s s a g e message variable never get updated with new value, so printing the variable will re-print the same words.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...