0

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

5
  • 1
    total = int(age) + int(height) + int(weight) Commented May 10, 2019 at 6:15
  • 1
    int(6.25) will fail @Rakesh Commented May 10, 2019 at 6:23
  • @DeveshKumarSingh..In that case just use float() these are basics in python. Commented May 10, 2019 at 6:32
  • I think you missed my point @Rakesh, I was pointing the issue in your first comment :) Commented May 10, 2019 at 6:44
  • Hi @VishwjeetSingh you would need to convert each variable to an integer to add those up. Please consider checking my answer below :) Commented May 16, 2019 at 17:35

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Are you sure you can do int(6.25) ?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.