0

What is the difference between these two code?

total = 0 x = 1 while x <= 5: age = int(input()) if age > 3: total += 100 x += 1 print(total) total = 0 x = 1 age = int(input()) while x <= 5: if age > 3: total += 100 x += 1 print(total) The first code worked so well but the second one didn't. I just can't understand what is the problem with defining variable "age" before while? Please help me if you know.

12th Jul 2022, 9:10 AM
MOBIN MA
MOBIN MA - avatar
2 Answers
+ 4
in first code your input is in while, and because of your while works 5 times, you will be asked to input number five times, in second code your input is outside of the while so you will be asked to input age only one time (sry for my english)
12th Jul 2022, 9:45 AM
Ali Muhamedzhan
Ali Muhamedzhan - avatar
+ 3
# Hi, MOBIN MA # If you want to have the input() outside the loop, you can try something like this: total = i = 0 # input all ages on one line with space between, like: 2 5 7 ages = [int(i) for i in input().split()] while i < len(ages): if ages[i] > 3: total += 100 i += 1 print(total)
12th Jul 2022, 10:43 AM
Per Bratthammar
Per Bratthammar - avatar