0

Can anyone please explain me this

Question: You have a magic box that doubles the count of items you put, in every day. The given program takes the initial count of the items and the number of days as input. Task Write a program to calculate and output items' count on the last day. Solution: items = int(input()) days = int(input()) #your code goes here while days>0: items*=2 days-=1 print(items)

28th Apr 2022, 4:02 PM
Kaushik
Kaushik - avatar
2 Answers
+ 5
Hello Kaushik You take two user inputs: items and days (both are integer) while days > 0: -> the loop will end when days gets 0 items *= 2 -> items = items * 2 items gets doubled. days -= 1 -> days = days - 1 days gets reduced by 1. Let's say I enter 3 items and 2 days. days = 2 while condition returns true items = 3 * 2 = 6 days = 2 - 1 = 1 days = 1 while condition returns true items = 6 * 2 = 12 days = 1 - 1 = 0 days = 0 while condition returns false print(items) output: 12
28th Apr 2022, 4:14 PM
Denise Roßberg
Denise Roßberg - avatar
0
items = int(input()) days = int(input()) The simplest way to get to the correct answer from there, would be to use exponentiation. Total items, multiplied by 2 to the power of the number of days. total = items*(2**days)
29th Apr 2022, 6:03 PM
Iain Ozbolt
Iain Ozbolt - avatar