- 2

Python Core Lesson 26.2 "let's do some magic"

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. Sample Input 3 2 Sample Output 12 Explanation Day 1: 6 (3*2) Day 2: 12 (6*2) I am having a really hard time with this. The variable placement is not computing in side my mind. Items = int(input()) Days = int(input()) While days > 0: Days += 1 Items = days + items * 2 Print items I am generally having a difficult time with while loops I've completed 1 lesson but the other 4 are a pain. Thanks for your help. :)

10th Sep 2021, 10:55 PM
Michael
Michael - avatar
6 Respostas
+ 6
Ok, that's nearby 😉 while days > 0 is ok. But then you can't count up the days. This loop will never end. Count them down: days - = 1 Then double the items each day: items *= 2 You can't add items and days, that makes no sense. Then print items, but not in the loop! And take attention to name all variables lower case. Don't mix days and Days or items and Items.
10th Sep 2021, 11:16 PM
Coding Cat
Coding Cat - avatar
+ 2
Hello Stefan, thank you for your answer. Please, I don't understand the if days == 0: How could I interpretate in order to understand it? Thank you
22nd Dec 2021, 10:20 AM
josé manuel
josé manuel - avatar
+ 1
This works for me: items = int(input()) days = int(input()) #your code goes here while days > 0: days -= 1 items *= 2 if days == 0: print(items) If you have any question just ask!
15th Nov 2021, 8:39 PM
Stefan Bartl
Stefan Bartl - avatar
0
That totally worked. Ugh I was so close. I had tried the items *= 2 before but never did the days -= 1 ! That's so weird! I also think it's odd that in some of the examples before this lesson they printed within the loop. I've been working on this loop for several days, it's almost as if I kept pushing further away the more I tried. I have to remember to keep it simple. Thank you so much!
10th Sep 2021, 11:23 PM
Michael
Michael - avatar
0
Your are welcome. And I know that. Some things took a while. But one day it becomes easy. Keep on coding 🙋‍♂️
10th Sep 2021, 11:31 PM
Coding Cat
Coding Cat - avatar
0
Hi Stefan Bartl, ntmy, can you explain a little more your code?? Tnx
14th Feb 2022, 3:13 AM
JOSE FLORES
JOSE FLORES - avatar