+ 1

Need help with a code.

I'm trying to add the variable to the same line as the quote in the line above it, but just putting the + then the sum afterwards gives me an error. The one below works but I have to go to the next line and put print (sum) Instead of just Print ("quote " + sum) Which I figured would work. Anyone know what I'm doing wrong? If you need me to replicate the version that's giving me the error just let me know. The one below is the altered version that works but it makes me put the variable "sum" on a separate line. https://code.sololearn.com/c0W85tslb4Iz/?ref=app

30th Jul 2019, 2:31 AM
AR0
AR0 - avatar
6 Réponses
+ 7
If you don't use '+', you don't need to convert c to a string. The print() function puts a space between items separated by commas, so e.g. print(3, 4, 5) outputs 3 4 5, so the last line of your code could be be print("The sum of these numbers is", c) Btw spaces around commas in the print() function are ignored, so print(1,2) outputs the same as print(1 , 2) and print(1, 2). The last format is considered better Python coding style. 🙂
30th Jul 2019, 3:18 AM
David Ashton
David Ashton - avatar
+ 3
It is not recommendable to use functions (e.g., sum) as variable names as it can mess up with your code. c = int(a) + int(b) print("The sum of these numbers is " + str(c))
30th Jul 2019, 2:36 AM
Diego
Diego - avatar
+ 3
The problem wasn't that "sum" was a function, but that you were trying to concatenate a string with an integer. The "don't use 'sum' as a variable name" part was more of a recommendation.
30th Jul 2019, 2:52 AM
Diego
Diego - avatar
+ 2
Thanks! I hadn't made the connection that sum was a function. It worked! :D
30th Jul 2019, 2:41 AM
AR0
AR0 - avatar
+ 1
Oh so I had to tell it to change the integer to display it as a string basically?
30th Jul 2019, 2:53 AM
AR0
AR0 - avatar
+ 1
Whoops the link is showing the edited version now lol. But awesome that theres multiple ways to do it, thanks
30th Jul 2019, 2:57 AM
AR0
AR0 - avatar