+ 5
What's wrong with this code ? ( Beginner)
#addition of numbers name= input() num1= int(input()) #pick a number num2= int(input()) #pick a number sum = num1 + num2 print (sum) print (name + " , " + num1 + " + " + num2 " = " + sum + " !") print ("eurika !!")
14 ответов
+ 9
If you're not familiar with f-string, just don't use + in your print. Comma-separate them instead. You can't add strings and ints, but you can pass them as comma separated arguments in print.
Also, don't use sum as variable name. Sololearn does not give you warnings, but Pydroid and other decent IDEs will warn you that sum is a Python method name and you are redefining it. You don't want to do that.
name= input()
num1= int(input()) #pick a number
num2= int(input()) #pick a number
total = num1 + num2
print(total)
print(name, "," , num1, "+", num2, "=", total, "!")
print("eurika !!")
but yes, as Lothar suggested, learn about f-strings. It will give you much more control in printing and string formatting.
+ 8
name is not defined
you need to convert the numbers to strings before concatenating
there is a + missing in the 2nd print
+ 7
Odobeatu Emmanuella ,
to get the output string in a more simpler way, we can use a so called `f-string` (string interpolation). this needs a bit learning new things, but as you can see in the samples, the definition of the output is less confusing, and we do not need to convert numerical values to a string first.
this link leads to a tutorial about string handling:
https://stackabuse.com/guide-to-strings-in-JUMP_LINK__&&__python__&&__JUMP_LINK/
to get to the section where f-strings are explained, go to the menue of the browser, select `search in page` and insert this:
`Introduction to f-strings`
you will find explanations and code samples that you can use / modify for your own purpose.
+ 5
variable 'name' is not defined.
+ 5
Rizwan Hanif
I recommend you start with the html and web development course or python as they are the most simple and easier languages to learn.
Give it a try !
If you need any help , we're all here to support you but give it a try yourself first :)
+ 4
Thanks everyone
+ 4
Rizwan Hanif ,
what kind of help do you need?
this forum is meant for coding related questions and answers.
> so if you have any coding related questions or douts you can post them here. please make sure to follow the forum guidelines.
+ 4
Its 21 days
+ 3
where did name come from?
+ 3
READ my COMPLETE comment.
+ 3
Rizwan Hanif
The messaging option gets open after 30 days of when you join sololearn
https://www.sololearn.com/Discuss/2668917/?ref=app
+ 3
The problem is that you are adding two variables with opposite values.
The name variable is of string type and the number variables are of int type and these two types of variable values cannot be added together...
To solve the problem, you can convert int type to string
+ 2
I made a mistake
Updated it now
I can't get it to print the statement I want
+ 1
idk