0

The question I must complete is as follows;

After caffeine is absorbed into the body, 13% is eliminated from the body each hour. Assume an 8-oz cup of brewed coffee contains 130 mg of caffeine and the caffeine is absorbed immediately into the body. Write a program that allows the user to enter the number of cups of coffee consumed. Write an indefinite loop (while) that calculates the caffeine amounts in the body until the number drops to less than 65 mg

This is what I currently have

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

main()

The output must present a column with the amount of hours that have passed on the left and the remaining amount of caffeine on the right. I am looking for guidance as to where I should go from here. Thanks.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
  • Just put a `print` in the loop that shows the current caffeine level. – Barmar Oct 23 '18 at 21:34
  • The question is asked weirdly. What is the expected output? Amount of caffeine in the body at each and every second? How many time it takes to drop under 65mg? Etc. – phaazon Oct 23 '18 at 21:34
  • `while caff <= 65:` should be `while caff >= 65:` – roganjosh Oct 23 '18 at 21:35
  • Also, saying it's an indefinite loop is pretty odd, because it's _not_ indefinite, it clearly terminates – Athena Oct 23 '18 at 21:35
  • If I attempt to print the variable caff within the loop, the program terminates after the input is given. Putting it outside the loop prints only the amount of caffeine in how ever many cups is entered into the input. –  Oct 23 '18 at 21:36

2 Answers2

1

You need another variable counting the number of hours. Then just print the two variables in the loop.

You also need to invert the test in the while. You want to keep looping while the amount of caffeine is at least 65 mg.

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()
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You just need to fix the while loop and print the results.

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()
Athena
  • 3,200
  • 3
  • 27
  • 35
  • Also, `caff -= caff * float(0.13)` can be re-rewritten more effectively as `caff *= float(0.87)`. – phaazon Oct 23 '18 at 21:37