5. Interrupting the repetition¶
Calculate the sum of integers until zero is entered
Calculate the average of entered numbers until zero is entered
You have attempted of activities on this page
Calculate the sum of integers until zero is entered
System Message: ERROR/3 (/home/runner/work/PyZombis/PyZombis/_sources/lectures/TWP15/TWP15_5_en.rst, line 8)
Error in “activecode” directive: unknown option: “class”.
.. activecode:: python
:class: python
:nocodelens:
:stdin:
sum = 0
while True:
x = int(input("Enter a number (0 to exit): "))
if x == 0:
break
sum = sum + x
print("Sum: %d" %sum)
Calculate the average of entered numbers until zero is entered
System Message: ERROR/3 (/home/runner/work/PyZombis/PyZombis/_sources/lectures/TWP15/TWP15_5_en.rst, line 26)
Error in “activecode” directive: unknown option: “class”.
.. activecode:: python
:class: python
:nocodelens:
:stdin:
sum = 0
n = 0
while True:
x = int(input("Enter a number (0 to exit): "))
if x == 0:
break
else:
n = n + 1
sum = sum + x
print("Average: %5.2f" %(sum / n))