我是编码新手,仅在CS课程的第三周。 我正在做一个分配,我们必须创建一个嵌套的条件,然后使用逻辑运算符将其简化为单个条件。 我相信我已经正确地做到了,但是即使输出少于给定的数量,我简化的条件语句也只会选择第一个选项。
嵌套条件:
def steps(s):
return .04*s
#the average amount of calories burned per step is .04
walk = steps(6000)
#The variable walk is used to show that my walk was 6,000 steps
hike = steps(7000)
tdee = 1300
#tdee is your Total Daily Energy Expenditure.
day = (tdee + walk + hike)
#this if else statemnet is going to decide of you have burned the recomended number of calories
for the entire day
if day > 2000:
print("Success!")
else:
if day > 1700:
print("Almost!")
else:
if day > 1500:
print("Try again.")
else:
print("Failure.")
简化的单条件:
def steps(s):
return .04*s
#the average amount of calories burned per step is .04
walk = steps(500)
#The variable walk is used to show that my walk was 6,000 steps
hike = steps(7000)
tdee = 1300
#tdee is your Total Daily Energy Expenditure.
day = (tdee + walk)
#this if else statement is going to decide if you have burned the recommended number of calories
for the entire day
if day >= 2000 or >= 1700:
print("You are within the recommended calorie range.")
else:
print("You are under the recommended calorie range. Try again.")
使用给出的值,它应该给出的响应表明您“低于”建议的数量,但事实并非如此。 (注意我在任何地方都使用python可能也很重要。)