Python Forum

Full Version: simple string & input problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
im building a bmi calculator, very new to programming and i have this problem
[spoiler]
[python][b]name = input("what's your name ")

height = input ("what's your height " + name )
print (height)

weight = input ("what's your weight" + name)
print (weight)

[color=#2ECC71]bmi = weight /(height**2)[/color]
print(bmi)
Problem:: when i write my input there is no space in between the input (exampel "what's your name?Joey tribbiani"

and lastly, the biggest problem is that i can't run my code. (line bmi=weigt/(heigt**2)...) i don't know why.
this is my first real (problem im trying to solve) help appreciated.
The first problem can be done with string formatting:

height = input('What is your height, {}? '.format(name))
or f string in 3.6+:

height = input(f'What is your height, {name}? ')
The calculation problem is because input returns a string. You need to convert to integer with int():

height = int(input('What is your height, {}? '.format(name)))
(Jun-23-2019, 11:32 AM)ichabod801 Wrote: [ -> ]The first problem can be done with string formatting:

height = input('What is your height, {}? '.format(name))
or f string in 3.6+:

height = input(f'What is your height, {name}? ')
The calculation problem is because input returns a string. You need to convert to integer with int():

height = int(input('What is your height, {}? '.format(name)))

is there a easier way for the first problem? btw thank you so much for the help.
(Jun-23-2019, 01:49 PM)kungshamji Wrote: [ -> ]is there a easier way for the first problem? btw thank you so much for the help.

This is the easy and the right way. Wrong way would be adding + “ “
height = input('What is your height, {}? '.format(name))  # 56 characters
height = input(f'What is your height, {name}? ')          # 48 characters
height = input('What is your height ' + name + ' ')       # 52 characters
f-strings are the easy way. Note that adding another variable to this would add 2 characters plus the variable name to the format and f-string techniques, and 8 characters (4 spaces, 2 plusses, 2 quotes) to the addition technique. So in general, format will be easier than addition as well. And depending on implementation, addition can be less efficient.
(Jun-23-2019, 02:38 PM)ichabod801 Wrote: [ -> ]
height = input('What is your height, {}? '.format(name))  # 56 characters
height = input(f'What is your height, {name}? ')          # 48 characters
height = input('What is your height ' + name + ' ')       # 52 characters
f-strings are the easy way. Note that adding another variable to this would add 2 characters plus the variable name to the format and f-string techniques, and 8 characters (4 spaces, 2 plusses, 2 quotes) to the addition technique. So in general, format will be easier than addition as well. And depending on implementation, addition can be less efficient.

now i understand you have really helped me i used the last one and it worked out. Note that I just started learning, and i appreciate the help.