+ 1

Help - Python 26.2 - Let's Do Some Magic

Can anyone explain to me the third line of code? Why has the 'coach' used a "for" loop for a "while" loop question? I know that they are somewhat interchangeable, albeit "for" loops are more succinct. Where has the "i" come from? How can the program identify what "i" is without prior allocation? What does "range(days)" mean? Is "range" a function or a variable? If it's the latter how come it too has not been given a value? Can someone show me a solution to the question using "while"? 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. Sample Input 3 2 Sample Output 12 Explanation Day 1: 6 (3*2) Day 2: 12 (6*2) CODE items = int(input()) days = int(input()) for i in range(days): items *= 2 print(items)

3rd Nov 2021, 9:24 AM
Lee Vitout
Lee Vitout - avatar
10 odpowiedzi
+ 3
it uses a for loop because it knows exactly how many times it needs to loop. while loops are only for when you don't know
3rd Nov 2021, 10:16 AM
Slick
Slick - avatar
+ 3
Compiler checks only the output whatever solution we submit. print(42) is the solution for all challenges if expected output is 42. All you need to run the while loop until variable days becomes 0.
3rd Nov 2021, 12:03 PM
Simba
Simba - avatar
+ 2
range is a function that generates the given number of integers stating from zero. ex:- range(5) means it generates 0, 1, ,2, 3, 4. here 5 doesn't include.(syntax:- range(stop)) range(2, 5) generates 2, 3, 4. here starting number includes but not ending number(syntax(range(start, stop)) range(2, 9, 3) generates 2, 5, 8. here 3 is step i.e. distance between two consecutive numbers less than 9(syntax:- range(start, stop, step)) for i in range(days): range(days) generates given number of integers and "i" refers to those numbers in the generated order one by one here is while loop https://code.sololearn.com/cl6L61W8130g
3rd Nov 2021, 2:19 PM
Krishnam Raju M
Krishnam Raju M - avatar
+ 1
Not sure, I didn't make the app. And just use a counter variable in the while statement
3rd Nov 2021, 10:25 AM
Slick
Slick - avatar
+ 1
Based on the lesson I went with while days >= 1: items = items * 2 days = days - 1 print(items)
13th Dec 2021, 11:11 PM
Daniel White
Daniel White - avatar
0
But why use a for loop before the lesson on for loops? How could one solve this problem with a while loop?
3rd Nov 2021, 10:22 AM
Lee Vitout
Lee Vitout - avatar
0
try this; items = int(input()) days = int(input()) #your code goes here i = 1 while i <= days: items *= 2 i = i + 1 print(items)
9th Sep 2022, 4:25 PM
Anime Zone
Anime Zone - avatar
0
without declaring a new variable: while days > 0: items = items * 2 days =days- 1 print(items) you can make it faster, but it works!
15th Sep 2022, 10:07 AM
David Bauer
0
While days>0: Items*=2 days-=1 Print(Items)
11th Oct 2022, 4:25 PM
Sus HaNt
Sus HaNt - avatar
- 1
n=int(input('Enter the number of items you put every day: ')) p=int(input('Enter the number of days: ')) r=0 q=2*n for i in range (0,p): r=q+r print('Items present on last day: ',r) Very simple code
22nd Dec 2021, 11:01 AM
SANVIKA