Python 3.3.3代码帮助。卡路里剩余

时间:2015-10-22 16:22:21

标签: python

FOR /F "delims=" %%G IN ('dir /a-d /b /s bk.bat') DO CALL "%%~G"

这是我尝试写的代码的开头,其中某人输入了他们吃过的性别,年龄和卡路里。我的老师给我的数据显示了每个年龄组和性别应该吃的卡路里数量。然后根据他们的年龄和性别,减去他们吃的卡路里和他们应该吃的价值。它非常简单,所以包括的唯一年龄组是11-14和15-18。问题都运行良好,但我无法运行代码的主要部分。它在Python 3.3.3

2 个答案:

答案 0 :(得分:0)

您需要对代码进行以下更改。

 print("Are you male or female?")
 gender = input().lower()
 print("How old are you?") 
 age = int( input() )
 #convert input to int

 print("How many calories have you eaten today?")
 calories = int( input() )
 #convert input to int
 if age in [ x for x in range(10,15)  ] and gender == "male":     
     print(2230 - calories)

答案 1 :(得分:0)

您的代码中存在很多问题。一,当你试图将你得到的值作为年龄/卡路里进行比较时,你不是首先将它们转换为int,而是你无法比较int和字符串。此外,gender == (Male)会将变量gender与变量Male进行比较。

这是一个清理版本:

 print("Are you male or female?")
 gender = input()
 while True:
     try:
         age = int(input("How old are you?"))
     except ValueError:
         print('Please enter a number for your age')
         continue
     else:
         break

 while True:
     try:
         calories = int(input("How many calories have you eaten today?"))
     except ValueError:
         print('Please enter a number for your calories eaten today')
         continue
     else:
         break

 if (10 < age < 15) and gender == 'male':     
     print(2230 - calories)