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
  f string concatenation problem growSeb 3 250 Jun-28-2021, 05:00 AM
Last Post: buran
Question Problem with string and \n Falassion 6 338 Jun-15-2021, 03:59 PM
Last Post: Falassion
  Creating new column with a input string drunkenneo 2 464 Apr-14-2021, 08:10 AM
Last Post: drunkenneo
  Problem restricting user input in my rock paper scissors game ashergreen 6 889 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  how to deal with problem of converting string to int usthbstar 1 453 Jan-05-2021, 01:33 PM
Last Post: perfringo
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 703 Oct-19-2020, 04:16 AM
Last Post: deanhystad
  single input infinite output problem Chase91 2 557 Sep-23-2020, 10:01 PM
Last Post: Chase91
  string problem Mathisdlg 6 1,045 Aug-05-2020, 09:31 AM
Last Post: Mathisdlg
  simple f-string expressions to access a dictionary Skaperen 0 517 Jul-15-2020, 05:04 AM
Last Post: Skaperen
  Problem with the input marios 4 725 May-03-2020, 01:01 PM
Last Post: marios

Forum Jump:

User Panel Messages

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