Lpthw: Ex9: line 6 and 7

Hello, I am new here. It is quite intimidating to share my questions with people since probably I don’t know what I am talking about. No programming/computer science background.

I rewrote the codes in Ex9 in lpthw, p36 using %r. See below. It doesn’t work. Why wouldn’t it work?

days = “Mon Tue Wed Thu Fri Sat Sun”
months = “Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug”

print “Here are the days: %r.”, % days
print “Here are the months: %r.”, % months

Thank you.

1 Like

You have unsupported operand type(s). Not too familiar with this yet as I am still making progress. But I messed around a little more with your code and got the results! Take a look :slight_smile: - CheersSolution

Take a look at your lines of code. Put a comma (,) right before the %. In my code I have just %.

Also, you can do this to clean up your code in the forum:

[code]
days = “Mon Tue Wed Thu Fri Sat Sun”
months = “Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug”

print “Here are the days: %r.”, % days
print “Here are the months: %r.”, % months
[/code]

Hello R,

Thanks for your reply and I apologize for the late response. I tried your code and it worked but I tried different codes without () after print and I also tried %r instead of %s. When I tried %r, n (new line) did not work. Months were shown in one line. And the codes worked without (). See below. Thanks!^^

trial1)
days = “Mon Tue Wed Thu Fri Sat Sun”
months = “Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug”

print “Here are the days: %s” % days
print “Here are the months: %s” % months

Here are the days: Mon Tue Wed Thu Fri Sat Sun
Here are the months: Jan
Feb
Mar
Apr
May
Jun
Jul
Aug

trial2)
days = “Mon Tue Wed Thu Fri Sat Sun”
months = “Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug”

print “Here are the days: %r” % days
print “Here are the months: %r” % months

Here are the days: ‘Mon Tue Wed Thu Fri Sat Sun’
Here are the months: ‘Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug’

Hi Zed,

Thanks for your feedback! I tried your suggestion and yes it worked. I checked the codes in EX6 and there was no comma in the code in Ex6. So I didn’t pay attention to it. This is difficult since when I was checking the code, I couldn’t find the difference between mine and the one in EX6… Please see the codes that I tried in my response to R’s comment. Thanks! Suhwa

In regards to the parantheses i have to use them otherwise I get a SyntaxError :slight_smile: Other than that I was able to get the same results as you posted above.

If you get a syntax error you may be using the wrong python. Do this:

python --version

If you are using my Python 2 book then it should say something like 2.7.x (where x can be any number). If you are using my Python 3 book then it should say 3.6.x (where x can be anything).

1 Like