Home > Blockchain >  im trying to get the sum of the variables but when i try to get the output the answer says 812 but i
im trying to get the sum of the variables but when i try to get the output the answer says 812 but i

Time:10-01

quilt_width = 8
quilt_length = 12

print("Number of squares you'll need to create a quilt: "  

str(quilt_width)   str(quilt_length))

CodePudding user response:

print("Number of squares you'll need to create a quilt: "  

str(quilt_width   quilt_length))

CodePudding user response:

run this my friend :

quilt_width = 8 ;quilt_length = 12
sum_quilt = quilt_width  quilt_length

print("Number of squares you'll need to create a quilt: "   str(sum_quilt))

CodePudding user response:

Concatenation of strings causes the numbers as strings to join. In your case, "8" "12" becomes 812. You have to add the integers and then convert that to string.

The code is as follows:

quilt_width = 8 quilt_length = 12

print("Number of squares you'll need to create a quilt: "   str(quilt_width   quilt_length)

CodePudding user response:

Remember the function str converts a numeric variable into a text which we call string in python.

and adding two variables of string type will perform a concatenation. when you want to perform arithmetic operations you need to make sure the type of the variable is a number.

quilt_width = 8 #this is a number (integer)
quilt_length = 12 #this is a number (integer)
str(quilt_width)   str(quilt_length) # You are converting the numbers into text before adding them

This results in

"812"

The code below will return what you are expecting

quilt_width = 8 #this is a number (integer)
quilt_length = 12 #this is a number (integer)
quilt_width   quilt_length # returns 20 as a number
str(quilt_width   quilt_length) # This returns "20" as a string because first you add the numbers and then you convert the result to numbe

CodePudding user response:

To find the number of squares you would need to multiply the width by its length:

quilt_width = 8
quilt_length = 12

print(f"Number of squares you'll need to create a quilt: {quilt_width * quilt_length}")

Output:

96
  • Related