python条件运算符的嵌套如何使用,python - 使用逻辑运算符简化嵌套条件:逻辑运算符不起作用吗? - 堆栈内存溢出...

一名编程初学者在Python作业中遇到了一个问题,他们尝试将嵌套的条件语句简化为单个条件,但在实现过程中,简化后的代码在某些情况下无法正确输出。原始代码检查每日能量消耗是否达到推荐值,简化后的条件未能在总消耗低于推荐值时正确显示‘低于’的提示。
摘要由CSDN通过智能技术生成

我是编码新手,仅在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可能也很重要。)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值