0

What's wrong with line 7?Keeps showing me operand type error, I wrote them same as book. And what others possibly go wrong?

x= ("There are %d types of people") %10 binary= ("Binary") do_not= ("don't") y= ("Those who know %s & those who %s") %(binary,do_not) print (x) print (y) print("I said: '%r'.") %x print("Also I said: '%s'.") %y great= (" False") joke= ("Isnt it funny ") %r print(" joke") %great

23rd May 2019, 7:26 AM
Sheikh Tawsif Samin
Sheikh Tawsif Samin - avatar
11 Réponses
+ 7
There's no need to put a variable in parentheses when you declare it. x = 5, not x = (5) Whereas string formatting with % expects a tuple as argument, so you might want to change x= ("There are %d types of people") %10 to x = "There are %d types of people" % (10,).
23rd May 2019, 8:24 AM
Anna
Anna - avatar
+ 5
1. What swim said. 2. You probably copied this from a very old book on Python 2, right? I guess that's why you misplaced the parentheses in the print statement. Nowadays we don't use these old formatting styles. The format() and the more modern f-string are a lot more convenient. Check those out!
23rd May 2019, 8:20 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 3
Anna, for a single argument, I think we don't need to turn it into a tuple.
23rd May 2019, 8:32 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 3
Kishalaya Saha I agree that we don't necessarily have to convert a single argument to a tuple, but I think consistency is the key if we try to write clean and maintainable code (which should be everyone's goal). If I know that a function/method etc. expects a tuple, I'll try to give it a tuple. Even if it might be able to handle integers as well. But maybe I'm being "too German", I don't know 😏
23rd May 2019, 9:01 AM
Anna
Anna - avatar
+ 1
Actually am practising from 'Learn programming in hard way',did the same this done in that book and the error was shown...Maybe It's what the kishalaya said.... And thanx swim it worked.. Would you mind telling me what other things could go wrong if I keep working with pyth 2?
23rd May 2019, 8:27 AM
Sheikh Tawsif Samin
Sheikh Tawsif Samin - avatar
+ 1
So any pdf good book on python 3 would you want to suggest me? That'd be very helpful
23rd May 2019, 8:47 AM
Sheikh Tawsif Samin
Sheikh Tawsif Samin - avatar
+ 1
Thanx mate ~ swim ~
23rd May 2019, 9:02 AM
Sheikh Tawsif Samin
Sheikh Tawsif Samin - avatar