在python中使用不确定循环编写程序

2024-06-26 03:50:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须完成的问题如下:

当咖啡因被人体吸收后,13%的咖啡因会从体内排出 小时。假设一杯8盎司的煮咖啡含有130毫克咖啡因和 咖啡因会立即被人体吸收。编写一个允许用户 输入消耗的咖啡杯数。写一个不确定的循环 计算体内咖啡因的含量,直到这个数字降到65毫克以下

这就是我目前所拥有的

def main():
    cup = float(input("Enter number of cups of coffee:"))
    caff = cup * float(130)
    while caff <= 65:
        caff -= caff * float(0.13)

main()

输出必须显示一个列,左边是已经过去的小时数,右边是剩余的咖啡因量。我在寻求指导,告诉我该从这里走到哪里。谢谢。在


Tags: of用户maindef数字人体floatcup
2条回答

您只需要修复while循环并打印结果。在

def main():
cup = float(input("Enter number of cups of coffee:"))
caff = cup * float(130)
hours = 0
while caff >= 65:
    hours += 1
    caff -= caff * float(0.13)
    print("{0},{1}".format(hours, caff))
main()

你需要另一个计算小时数的变量。然后只打印循环中的两个变量。在

您还需要在while中反转测试。当咖啡因含量至少为65毫克时,你要保持循环。在

def main():
    cup = float(input("Enter number of cups of coffee:"))
    caff = cup * float(130)
    hours = 0
    while caff >= 65:
        caff -= caff * float(0.13)
        hours += 1
        print(hours, caff)

main()

相关问题 更多 >