Q. Find the error. Consider the following program:

 

a = int(input("Enter a value: "))

while a != 0:

    count = count + 1

    a = int(input("Enter a value")

print ("You entered", count, "value" )

 

It is supposed to count the number of values entered by the user until the user enters and then display the count (not including the 0). However, when the program is run, it crashes with the following error message after the first input value is read :

 

Enter a value: 14

Traceback (most recent call last):

  File "count.py", line 4, in <module>

    count = count + 1

NameError: name 'count' is not defined

>>> 

What change should be made to this program so that it will run correctly? Write the modification that is needed into the program above, crossing out any code that should be removed and clearly indicating where any new code should be inserted.

 

Answer =

 


count = 0
a = int(input("Enter a value: "))
while a != 0:
    count = count + 1
    a = int(input("Enter a value"))
print ("You entered", count, "value" )


Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post