如何在python中解决这个整数编程问题

时间:2015-05-22 12:10:46

标签: python python-3.x

我正在研究一个问题,要求我根据体重,身高等提出不同的人信息。我在第2-4行获取代码时遇到了一些问题,无法正确检查用户给出的输入是否是数字。代码如下:

print(""" Would you like to...
    1: measure your bmi by metric.
    2: measure you bmi by imperial.
    9: quit.""")
choice =int (input("please input a number"))
if [i for i in choice if i.isdigit()]:
    print (" please input a number")

elif choice == 1:
     weight = int(input("please input your weight in kilograms."))
     height = int(input("please input your height in cm."))
     BMI =(weight)/((height)/height)
     print (BMI)

     if BMI <= 18 :
         print("you are underweight")


     elif BMI >= 18.5 and BMI <=25:

         print("you got normal weight")
     elif BMI >=25.5 and BMI <=30:
         print("you are overweight")
     elif BMI >30.5:
         print ("you are obese")
     else:
         print("")


elif choice == 2:
     weight2 = int(input("please input your weight in pounds."))
     innches = int(input("please input your height in inches"))      

     BMI = ((weight2)/((innches)*(innches))*703)
     print (BMI)
     if BMI <= 18 :
         print("you are underweight")

     elif BMI >= 18.5 and BMI <=25:

         print("you got normal weight")
     elif BMI >=25.5 and BMI <=30:
         print("you are overweight")
     elif BMI >30.5:
         print ("you are obese")
     else:
         print("")        

elif choice == 9:
    exit()

else:
    print("put numbers")

有关如何使这项工作正确/更好的任何帮助/建议将不胜感激。谢谢:))

3 个答案:

答案 0 :(得分:1)

首先:我不明白这里的反馈意见。我们都从基础开始。这应该被接受而不是被抨击!

现在解决您的问题:您应该为您的用户输入编写单独的检查功能。但首先,您可以从这个简单的检查开始:

# this one for python 2
weight_input = raw_input("please input your weight in kilograms: ")

# this one for python 3
weight_input = input("please input your weight in kilograms: ")

try:
    weight = int(weight_input)  # trying to cast the user input into an integer
except ValueError:  # only called, if casting not possible
    print("That's not an int!")

提示:您只能通过input替换input_raw功能来实现。进一步阅读:http://cis.poly.edu/cs1114/pyLecturettes/raw_input-input.html

答案 1 :(得分:0)

当前问题

在这部分中,您将获得用户输入内容的字符串对象:

input("please input a number")

然后你要做的是从输入字符串中解析int

choice =int(input("please input a number"))

因此,如果用户输入了非数字,您将在该行收到错误,例如:

ValueError: invalid literal for int() with base 10

然后你尝试迭代新获得的int

[i for i in choice if i.isdigit()]

......这是不可能的:

TypeError: 'int' object is not iterable

即使它是可迭代的,作为列表理解的结果,你也会得到一个&#34; int&#34;元素的列表。 (但听起来很荒谬),为True电话返回isdigit()

意志将评估该列表:

if [i for i in choice if i.isdigit()]:
   ...

并且只要列出第一个&#34; int&#34;的元素,它就会变为现实。在新列表中,因为:

>>> bool([])
False
>>> bool([1])
True

解决方案

您可以尝试解决这个问题:

try:
    choice = int(raw_input("Enter number: "))
except ValueError:
    print("Please enter a valid number")

答案 2 :(得分:0)

我认为你不需要这一点:

if [i for i in choice if i.isdigit()]:
    print (" please input a number")

其意图不明确(是检查数字,还是试图循环错误,或什么?)。无论如何,如果输入已经转换为int,并且逻辑实际上被反转以进行数字检查,它就不起作用 - 这将打印包含1位或更多位的任何字符串的输出。您需要做的就是:

if not choice.isdigit():
    print("please enter a number")

无需迭代每个角色,但我离题了......

但是choice已经是前一行中int()的整数礼貌。您可以将其更改为:

choice =int (input("please input a number"))
if choice == 1:
     weight = int(input("please input your weight in kilograms."))
...

它会起作用。如果你想捕获非数字的输入,你可以这样做:

try:
    choice = int(input("please input a number"))
except ValueError:
    print("Enter digits only")

您应该应用相同的模式来检查其他值(如重量和高度)的输入,并且您应接受这些值的浮点数。

最后,看起来你应该将整个事情包装在一个循环中,这样用户可以有多个输入值。实际上,代码将在一次尝试后终止,在这种情况下提供&#34;退出&#34;选项似乎毫无意义。