+ 2
[Solved]What is wrong with this basic Python code? (error in line 4)
total = 0 i = 1 while i <= 5: age = int(input()) if age > 3: total += 100 else: continue i += 1 print(total)
7 Answers
+ 8
Your code asks atleast 5 inputs values greater than 3. So on no input, it throws EOF error.. Your code have logic flaw about using continue, it causing not execute when input not > 3
edit: also you can
total = 0
i = 1
while i <= 5:
age = int(input())
if age > 3:
total += 100
i += 1
print(total)
+ 5
since in the corresponding lesson the *break* and *continue* statement is explained, we shold use *continue* statement in our code. the increment of variable *i* has to be placed above *if* statement.
an other issue is the conditional expression:
...
if age > 3:
...
the task description says:
-> For children under 3 years old, the ticket is free (which will mean that children from the age of 3 (including) has to pay)
this would mean that the conditional should be: if age >= 3: (correct me if i am wrong)
unfortunately this issue will not be catched by any of the the test cases. so both versions pass successfully
total = 0
i = 1
while i <= 5:
age = int(input())
i += 1
if age >= 3:
total += 100
else:
continue
print(total)
+ 3
Orel Briga
because iteration value was incrementing before the continue command
if you use after else part then that will not work as Jayakrishna🇮🇳 said
So don't use continue because when if condition will satisfy then by default low number below 3 will be skipped.
+ 2
Remove else part.
Further read about continue..
logic error, it asks atleast 5 values that are greater than 3
+ 2
But th error is with line 4..,? How come?
+ 2
Probably you just didn't entered enough values (sololearn asks on start for all values for inputs) or you entered string/float
+ 2
Jayakrishna🇮🇳 it was solved when I put the iteration variable right after the "while"