simple string & input problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: simple string & input problem (/thread-19323.html) |
simple string & input problem - kungshamji - Jun-23-2019 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. RE: simple string & input problem - ichabod801 - Jun-23-2019 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))) RE: simple string & input problem - kungshamji - Jun-23-2019 (Jun-23-2019, 11:32 AM)ichabod801 Wrote: The first problem can be done with string formatting: is there a easier way for the first problem? btw thank you so much for the help. RE: simple string & input problem - perfringo - Jun-23-2019 (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 + “ “
RE: simple string & input problem - ichabod801 - Jun-23-2019 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 charactersf-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. RE: simple string & input problem - kungshamji - Jun-23-2019 (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 charactersf-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. |