0
While loop question
I wrote the following code to practice while loops, however Iâm getting an unexpected answer for the number 5. I thought the code would print out "you can count that on one hand". Instead itâs outputting "you need two hands to count that many". Can someone explain why itâs the latter and not the former? The other numbers work as expected. Thanks. Code: i = 1 while i <= 10: print(i) i = i + 1 if i <= 5: print("you can count that on one hand") else: print("you need two hands to count that many") print("Great job!")
2 Answers
+ 3
you are incrementing i before if,
so for 1. test you have i=2
then for 5. test you have i=6
move i = i + 1 after if else
0
That worked, thanks zemiak!