Comments on my ps2b code? http://pastebin.com/rCX2nVh7 NOTE: You'll see a mix of string concatenation and string formatting with '%i' and '%s' in the printed answers at the end. I was originally going to do it all in string formatting but I kept getting an error saying that Python couldn't print the integers in the string. The syntax was solid though, so I'm guessing this had something to do with those integers being floats? I just changed it so the numbers were converted to string first and then concatenated to get past the error. Never bothered to change the first print statement too.
My bad! This code is actually from ps1b, not ps2b! LOL, forgot the whole counting-from-zero thing.
Hi there. Just to point out quickly; floats are not actually integers, ints are the non-floats, the whole numbers. Anyway, have you learnt how to use the string .format() method yet? You can use that for creating new strings really well. a = 50 b = 5.5 c = 'The number {0} is an example of an int, whilst {1} is a float.'.format(a, b) You can also use the str() function within concatenation expressions. cheesy_pop = 'ABC ~ It's easy like ' + str(123) I'll try and find time to have a proper look through your code soon, I'm just a bit busy at the moment.
Right! I was using 'integer' loosely there for a second without regard for it's Python meaning, my bad! And I haven't yet learnt that method. I'm going to look into that right away. Thanks man.
No worries.
You will get errors if you try and concatenate a non-string into a string expression. >>>string = 'abc' + 5 # this will throw one But the print statement will convert non-strings implicitly. >>>print 'abc', 5 abc 5 Note that print will not convert part of an expression. It just converts floats and ints and stuff to strings when they are single arguments in their own right >>>print 'abc' + 5 # another error coming up >>>print 'abc', 5, 5.5 abc 5 5.5 You can do legal expressions. >>>print 'abc' + 'cde', 5 + 5 abcdef 10 I personally never use %formatting, I never like it. I just use the format method or concatenation with str() calls. Add parenthesis, backslashes, and triple-quotes, and you can avoid ever using %formatting. a = 'Bob' print 'Hello ' +str(a)+ ' how are you?'
Join our real-time social learning platform and learn together with your friends!