0
Why does the input function HAVE to be inside the while loop?
i = 0 y = 100 while i < 4: x = input() if x == "hit": y = y + 10 i = i + 1 else: y = y - 20 i = i + 1 print(y)
2 Réponses
+ 3
Because the code will ask you for 4 inputs. As you probably had learned, a loop is a convenient way to deal with repetitive tasks. Exactly like this where 4 inputs will be read in, which decide what changes should happen to <y> value (increased or decreased)
Without placing the input() in a loop, you'd be copy-pasting the code block manually, which is rather dull and boring.
+ 2
Having the input inside the loop means that input is taken in every iteration, not just once.
According to this code, the value of i is increased in every iteration too. So the loop will execute exactly 4 times.