Python Forum
simple string & input problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
simple string & input problem
#1
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.
Reply
#2
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)))
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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.
Reply
#4
(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 + “ “
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using string input for boolean tronic72 3 635 Nov-01-2023, 07:48 AM
Last Post: Gribouillis
  problem in using input command akbarza 4 999 Oct-19-2023, 03:27 PM
Last Post: popejose
  problem in entering address of a file in input akbarza 0 619 Oct-18-2023, 08:16 AM
Last Post: akbarza
  A simple problem, how best to solve it? SuchUmami 2 683 Sep-01-2023, 05:36 AM
Last Post: Pedroski55
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,054 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  How to solve this simple problem? Check if cvs first element is the same in each row? thesquid 2 1,179 Jun-14-2022, 08:35 PM
Last Post: thesquid
  Convert string to float problem vasik006 8 3,269 Jun-03-2022, 06:41 PM
Last Post: deanhystad
Big Grin General programming question (input string)[ jamie_01 2 1,569 Jan-08-2022, 12:59 AM
Last Post: BashBedlam
  Problem with input after function luilong 10 4,021 Dec-04-2021, 12:16 AM
Last Post: luilong
Big Grin question about simple algorithm to my problem jamie_01 1 1,635 Oct-04-2021, 11:55 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020