Vishwjeet Singh
Vishwjeet Singh

Reputation: 1

Python : Not getting simple adding result

In my python code, all the result is fine except the "total". Instead of giving me the sum of variables it is giving me sum of strings?

age = '23'
height = '6.25' #feets
weight = '70' #kgs
eyes = 'hazel'
teeth = 'white'
hair = 'brown'

print(f"Lets talk about {name}." )
print(f"He's {height} feet tall.")
print(f"He's {weight} kilos heavy.")
print("Actually that's not too heavy.")
print(f"He's got {eyes} eyes and {hair} hairs.")
print(f"His teeth are usually {teeth} depending on the coffee.")


total = age + height + weight
print(f"If I add {age}, {height} and {weight}, I get {total}.")
PS E:\python programs> python p4.py
Lets talk about vishwjeet.
He's 6.25 feet tall.
He's 70 kilos heavy.
Actually that's not too heavy.
He's got hazel eyes and brown hairs.
His teeth are usually white depending on the coffee.
If I add 23, 6.25 and 70, I get 236.2570.

Please take a look at my program.See in image python program

Result

Result of program

Upvotes: 0

Views: 75

Answers (2)

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20490

Right now you are concatenating strings, by doing total = age + height + weight, hence you get total=236.2570., since '23'+'6.25'+70'='236.2570'

You need to convert the variables into int (age and weight) or float (height) accordingly, add them and then you will get your correct answer

name = 'vishwajeet'
age = '23'
height = '6.25' #feets
weight = '70' #kgs
eyes = 'hazel'
teeth = 'white'
hair = 'brown'

print(f'Lets talk about {name}.' )
print(f"He's {height} feet tall.")
print(f"He's {weight} kilos heavy.")
print("Actually that's not too heavy.")
print(f"He's got {eyes} eyes and {hair} hairs.")
print(f"His teeth are usually {teeth} depending on the coffee.")

#Convert age and weight to int and height to float, and add it
total = int(age)+ float(height) + int(weight)
print(f"If I add {age}, {height} and {weight}, I get {total}.")

The output will now be

Lets talk about vishwajeet.
He's 6.25 feet tall.
He's 70 kilos heavy.
Actually that's not too heavy.
He's got hazel eyes and brown hairs.
His teeth are usually white depending on the coffee.
If I add 23, 6.25 and 70, I get 99.25.

The other option is to define the variable at the start as int or float instead of string

name = 'vishwajeet'

#Define variables as int or floats instead of string
age = 23
height = 6.25 #feets
weight = 70 #kgs

eyes = 'hazel'
teeth = 'white'
hair = 'brown'

print(f'Lets talk about {name}.' )
print(f"He's {height} feet tall.")
print(f"He's {weight} kilos heavy.")
print("Actually that's not too heavy.")
print(f"He's got {eyes} eyes and {hair} hairs.")
print(f"His teeth are usually {teeth} depending on the coffee.")

total = age+ height + weight
print(f"If I add {age}, {height} and {weight}, I get {total}.")

The output will be same as above

Upvotes: 0

razdi
razdi

Reputation: 1440

All your variables are in a string format. So when you add them at the end, it turns into a concatenation rather than the expected addition. To get around it, you can either:

# set all variables as int/floats from the start
age = 23
height = 6.25
wright = 70

or you can:

# cast them as int/floats before adding
total = int(age) + float(height) + int(weight)

Upvotes: 2

Related Questions