+ 1

I need help with basic python

My code: a = 0.01 t = 0 b = 'qwertyuiopasdfghjklzxcvbnmqwae' for i in b: print(a) a = a*2 t = t+1 if t == 30: break Question: How could I make the code loop without needing to add as many characters in the variable 'b' as times it shall loop. Ex: If I set variable 'b' to 'abcd' it will repeat 4 times because there are 4 characters. Note: I heard someone saying that a cent doubling 30 times is 5 million, I tried to see it and I realized my code was not working as I hoped it to.

12th Oct 2022, 6:52 PM
NIM0 HQ
3 Respostas
+ 6
NIM0 HQ , thanks, now i understand. so you need the letters in variable *b* only as a kind of counter. but we do not need to use this line: #b = 'quert' modify the next line which is a for loop. we can use range() to generate 30 numbers to define the number of iterations. ... for b in range(0, 30): .... we can now also remove the last 2 lines of the code: ... #if t == 30: #break that's all.
12th Oct 2022, 8:01 PM
Lothar
Lothar - avatar
+ 5
NIM0 HQ , not quite clear what you wanted to achieve. can you give an output sample?
12th Oct 2022, 7:12 PM
Lothar
Lothar - avatar
+ 1
Lothar Yes. if the variable b is set to a string with 7 characters in it only the first 7 numbers will display I need to give the variable b 30 members in the string for it to display all 30 numbers a = 0.01 t = 0 b = 'qwert' for i in b: print(a) a = a*2 t = t+1 if t == 30: break this code displays the first 5 results (0.01, 0.02, 0.04, 0.08, 0.16) could I display all 30 results without giving the string b 30 members
12th Oct 2022, 7:14 PM
NIM0 HQ