Struggling with a Type Error on my coding for a basic calculation.

huangapple go评论63阅读模式
英文:

Struggling with a Type Error on my coding for a basic calculation

问题

以下是您要翻译的代码部分:

我似乎找不到我的代码有什么问题我将我的数字转换为浮点数然后使用它们并声明`bmi`为浮点数问题出现在最后一行的打印语句之前我收到以下错误

TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

我的代码中哪个变量被解释为字符串?

height = input("输入您的身高(以米为单位):")
weight = input("输入您的体重(以千克为单位):")

float(height)
float(weight)
bmi = weight / height ** 2
float(bmi)
print("您的BMI是:" + str(bmi))
英文:

I can't seem to find what is wrong with my code. I convert my numericals to floats, then use them and declare bmi as a float. The problem comes before the print statement in the last line. I get the following error:

TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

What variable in my code is the console reading as a string?

height = input("enter your height in m: ")
weight = input("enter your weight in kg: ")


float(height)
float(weight)
bmi = weight / height ** 2
float(bmi)
print("Your BMI is " + str(bmi))

答案1

得分: 2

以下是要翻译的内容:

实际发生的情况是身高和体重的转换值没有存储在任何地方,它们仍然是字符串,因此出现了错误。

尝试以下代码:

height = float(input("输入您的身高(以米为单位):"))
weight = float(input("输入您的体重(以千克为单位):"))

您可以将输入视为浮点数,而不是字符串,这将在计算BMI时自动返回浮点数值,而且不会出现错误。

或者

height = float(height)
weight = float(weight)

您可以再次将浮点值分配给变量,就像上面那样。

而且,您无需再次将BMI转换为浮点数,因为BMI将变为浮点数类型,因为两个变量都是浮点数。

英文:

what really is happening that the convert value of height and weight are stored anywhere and they remain as strings and so the error

try below

height = float(input("enter your height in m: ")) 
weight = float(input("enter your weight in kg: "))

you can take input as a float instead of string which will automatically return a float value for bmi while calculating
and it will not give any error also

or else

height = float(height) 
weight = float(weight) 

you can assign float values to var again like above

And also you dont need to convert bmi to float again as bmi will become type float since both var are floats

答案2

得分: 0

float(height)float(weight)heightweight 转换为浮点变量,但并未保存它们。因此,在转换后,heightweight 仍然是字符串,这就是您遇到此错误的原因。此外,bmi 不需要转换为浮点数,因为它已经是浮点数。以下是您的代码应该如何看起来的:

height = input("输入您的身高(以米为单位):")
weight = input("输入您的体重(以千克为单位):")

# 将身高和体重变量保存为浮点数
height = float(height)
weight = float(weight)

# bmi 不需要转换为浮点数,
# 因为它已经是浮点数,由于身高和体重变量是浮点数
bmi = weight / height ** 2

print("您的BMI为:" + str(bmi))
英文:

float(height) and float(weight) is converting height and weight into float variables, but it does not save them. So height and weight are still strings after the conversion, which is why you are getting this error. Also, bmi does not need to be converted into a float, because it already is a float. This is what your code should look like:

height = input("enter your height in m: ")
weight = input("enter your weight in kg: ")

# Saves the height and weight variables as floats
height = float(height)
weight = float(weight)

# bmi does not need to be converted into a float,
# because it already is a float since the weight and height
# variables are floats
bmi = weight / height ** 2

print("Your BMI is " + str(bmi))

huangapple
  • 本文由 发表于 2023年6月13日 01:17:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定