在 python 中转换高度:英尺和英寸的逗号让我很吃力
Converting height in python: commas for feet and inches giving me a rough time
所以我正在做一项家庭作业,处理将一组人的身高从“英尺和英寸”转换为米,然后对整个小组的身高进行平均。
除了英尺和英寸的输入,我的代码中的所有内容都工作正常。这是完整的代码:
def heights():
conversion = .0254
group = int(input('How many people are in your group? '))
for people in range(1, group+1):
print('\n''Person Number', people)
feet = range(10)
inches = range(12)
a = int(input('Enter your height in feet inches, separated by a comma: '))
meters = a*conversion
print("Your height in feet and inches: ", a,"'",'"')
print("Your height in meters is: ", meters)
print('\n' "The average height of your group in feet and inches is: ", a/group)
print("The average height of your group in meters is: ", a/meters)
所以对于第 8 行,它从 a = 开始,
如果用户输入他们的身高让我们说:6,2。我的程序崩溃了
但是如果你只做 74,就好像所有的单位都是英寸,代码运行起来很流畅,而且效果很好。
关于如何让它工作的任何提示?我尝试添加一个 .split(',')
函数,但这只会再次崩溃。它基本上必须看起来像:
以英尺和英寸为单位输入您的身高:6、2
你的身高以英尺和英寸为单位是 6' 2"
你的身高是 1.87
我添加了 feet = range 认为这样的东西会起作用,截至目前,它在我的代码中什么都不做,它大部分都在那里所以我在玩弄并试图弄清楚它时不会忘记
当您输入以逗号分隔的值时,input
会将其解释为元组。
因此,如果输入是 6
,a
的值将是整数 6
。但是如果输入是6,2
,a
的值就会是一个元组(6, 2)
.
input
将 eval
评价你的表情。
更简单的方法是使用 raw_input
,它将简单地将输入作为字符串。
a = raw_input('Enter your height in feet inches, separated by a comma: ').split(',')
a = [int(_) for _ in a]
然后你可以检查数组的长度a
,看看输入中是否有逗号。
编辑:对于 python3,只需将上面代码中的 raw_input
替换为 input
即可。
如果这样做,您会遇到什么错误?
要处理'6, 2'(例如)的字符串输入,您确实需要解析该字符串。一种方法是 - 正如您提到的那样 - 在逗号上拆分字符串。之后,您将获得字符串组件列表:['6', '2']
然后您可以通过索引访问每个组件(列表中的项目)。相关位是:
height = a.split(',')
feet = int(height[0])
inches = int(height[1])
然后你可以计算总英寸(英尺*12 + 英寸),转换为米,平均值等
你得到的错误(在下面的评论中)是因为你试图将输入字符串转换为 int(在你的语句 int(input(...
中),但字符串 '6,2' 可以' 被转换为整数,因此出现错误。首先,您必须将它的各个部分分开,然后进行转换,如上面的代码片段所示。
要进行转换,您需要计算总英寸数,然后再转换为公制。所以从上面的代码片段开始:
tot_inches = feet*12 + inches
meters = tot_inches * 0.0254
至于 input
与 raw_input
,了解您使用的 Python 版本会有所帮助,因为 raw_input
是 2.X功能(在 3.X 中不可用)。
最后,为了处理平均值,您需要保留所有输入高度的总和 运行。例如,在你的函数开始附近,初始化一个变量 tot_inches = 0
,然后在每次输入后,像这样增加 tot_inches
:
tot_inches = tot_inches + (12*feet + inches)
然后,在计算平均身高时,您可以执行 tot_inches/group
,这是以英寸为单位的平均身高,然后您可以将其转换为英尺和英寸。将它们放在一起:
def heights():
conversion = .0254
group = int(input('How many people are in your group? '))
# initialize running total
tot_inches = 0
for people in range(1, group+1):
print('\n''Person Number', people)
a = input('Enter your height in feet inches, separated by a comma: ')
height = a.split(',')
feet = int(height[0])
inches = int(height[1])
meters = (12*feet + inches)*0.0254
tot_inches += (12*feet + inches)
print ("Your height in feet and inches: ", feet,"'", inches,'"')
print ("Your height in meters is: ", meters)
print('\n' "The average height of your group in inches is: ", tot_inches/group)
print ('The average height in feet and inches is: ', tot_inches/(group*12), (tot_inches/group)%12
print("The average height of your group in meters is: ", (tot_inches*0.0254)/group)
所以我正在做一项家庭作业,处理将一组人的身高从“英尺和英寸”转换为米,然后对整个小组的身高进行平均。
除了英尺和英寸的输入,我的代码中的所有内容都工作正常。这是完整的代码:
def heights():
conversion = .0254
group = int(input('How many people are in your group? '))
for people in range(1, group+1):
print('\n''Person Number', people)
feet = range(10)
inches = range(12)
a = int(input('Enter your height in feet inches, separated by a comma: '))
meters = a*conversion
print("Your height in feet and inches: ", a,"'",'"')
print("Your height in meters is: ", meters)
print('\n' "The average height of your group in feet and inches is: ", a/group)
print("The average height of your group in meters is: ", a/meters)
所以对于第 8 行,它从 a = 开始,
如果用户输入他们的身高让我们说:6,2。我的程序崩溃了
但是如果你只做 74,就好像所有的单位都是英寸,代码运行起来很流畅,而且效果很好。
关于如何让它工作的任何提示?我尝试添加一个 .split(',')
函数,但这只会再次崩溃。它基本上必须看起来像:
以英尺和英寸为单位输入您的身高:6、2 你的身高以英尺和英寸为单位是 6' 2" 你的身高是 1.87
我添加了 feet = range 认为这样的东西会起作用,截至目前,它在我的代码中什么都不做,它大部分都在那里所以我在玩弄并试图弄清楚它时不会忘记
当您输入以逗号分隔的值时,input
会将其解释为元组。
因此,如果输入是 6
,a
的值将是整数 6
。但是如果输入是6,2
,a
的值就会是一个元组(6, 2)
.
input
将 eval
评价你的表情。
更简单的方法是使用 raw_input
,它将简单地将输入作为字符串。
a = raw_input('Enter your height in feet inches, separated by a comma: ').split(',')
a = [int(_) for _ in a]
然后你可以检查数组的长度a
,看看输入中是否有逗号。
编辑:对于 python3,只需将上面代码中的 raw_input
替换为 input
即可。
如果这样做,您会遇到什么错误?
要处理'6, 2'(例如)的字符串输入,您确实需要解析该字符串。一种方法是 - 正如您提到的那样 - 在逗号上拆分字符串。之后,您将获得字符串组件列表:['6', '2'] 然后您可以通过索引访问每个组件(列表中的项目)。相关位是:
height = a.split(',')
feet = int(height[0])
inches = int(height[1])
然后你可以计算总英寸(英尺*12 + 英寸),转换为米,平均值等
你得到的错误(在下面的评论中)是因为你试图将输入字符串转换为 int(在你的语句 int(input(...
中),但字符串 '6,2' 可以' 被转换为整数,因此出现错误。首先,您必须将它的各个部分分开,然后进行转换,如上面的代码片段所示。
要进行转换,您需要计算总英寸数,然后再转换为公制。所以从上面的代码片段开始:
tot_inches = feet*12 + inches
meters = tot_inches * 0.0254
至于 input
与 raw_input
,了解您使用的 Python 版本会有所帮助,因为 raw_input
是 2.X功能(在 3.X 中不可用)。
最后,为了处理平均值,您需要保留所有输入高度的总和 运行。例如,在你的函数开始附近,初始化一个变量 tot_inches = 0
,然后在每次输入后,像这样增加 tot_inches
:
tot_inches = tot_inches + (12*feet + inches)
然后,在计算平均身高时,您可以执行 tot_inches/group
,这是以英寸为单位的平均身高,然后您可以将其转换为英尺和英寸。将它们放在一起:
def heights():
conversion = .0254
group = int(input('How many people are in your group? '))
# initialize running total
tot_inches = 0
for people in range(1, group+1):
print('\n''Person Number', people)
a = input('Enter your height in feet inches, separated by a comma: ')
height = a.split(',')
feet = int(height[0])
inches = int(height[1])
meters = (12*feet + inches)*0.0254
tot_inches += (12*feet + inches)
print ("Your height in feet and inches: ", feet,"'", inches,'"')
print ("Your height in meters is: ", meters)
print('\n' "The average height of your group in inches is: ", tot_inches/group)
print ('The average height in feet and inches is: ', tot_inches/(group*12), (tot_inches/group)%12
print("The average height of your group in meters is: ", (tot_inches*0.0254)/group)