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)

21st Jan 2023, 5:45 AM
Rutherford
2 Answers
+ 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.
21st Jan 2023, 6:14 AM
Ipang
+ 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.
21st Jan 2023, 6:14 AM
Tibor Santa
Tibor Santa - avatar