0% found this document useful (0 votes)
9 views5 pages

Python Loop Exercises for Beginners

The document provides two exercises for practicing loops in Python. The first exercise involves creating a program that reads integers until zero is entered, counting positive and negative numbers, calculating their sum, and finding the average. The second exercise requires a program that asks the user to enter integers and decide whether to continue, while counting odd numbers, summing even numbers, and counting numbers that end with 5.

Uploaded by

Full name
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Python Loop Exercises for Beginners

The document provides two exercises for practicing loops in Python. The first exercise involves creating a program that reads integers until zero is entered, counting positive and negative numbers, calculating their sum, and finding the average. The second exercise requires a program that asks the user to enter integers and decide whether to continue, while counting odd numbers, summing even numbers, and counting numbers that end with 5.

Uploaded by

Full name
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exercises 4: Loop in Python

Exercise1: Write a Python program that uses a loop to read in a series


of integers of unknown length; the program should continue to read in
integer values (positive and negative), one at a time, until the user enter
a zero. After the loop ends (meaning that the user has entered 0), the
program should print out four values:
1- The number of positive (non-zero) integer that were entered.
2- The number of negative (non-zero) integer that were entered.
3- The sum of non-zero integer values that were entered.
4- The average of the (non-zero) values that were entered.
NB: Make sure that you can handle input will not be a character

Sample Execution:
Please enter an integer (0 to quit): -3
Please enter an integer (0 to quit): 15
Please enter an integer (0 to quit): 11
Please enter an integer (0 to quit): -9
Please enter an integer (0 to quit): abc
Error in input, try again
Please enter an integer (0 to quit): 2
Please enter an integer (0 to quit): 0
Positive integers entered: 3
Negative integers entered: 2
Sum: 16
Average: 3.2

1
Solution

pos = 0
neg = 0
sum = 0

while True :

nb = input("Please enter an integer (0 to quit): ")

try:
nb = int(nb)

if nb > 0:
pos += 1
elif nb < 0:
neg += 1
else:
break

sum += nb

except ValueError:
print("Error in input, try again")

print("Positive integers entered:", pos)


print("Negative integers entered:", neg)
print("Sum:", sum)
print("Avg:", sum/(neg+pos)) if neg+pos>0 else print("Error")

2
#second method

pos = neg = sum = 0

nb = input("Please enter an integer (0 to quit): ")

try:

nb = int(nb)

while nb!=0:

nb = input("Please enter an integer (0 to quit): ")

try:
nb = int(nb)

sum += nb

if nb > 0:
pos += 1
else:
neg += 1

except ValueError:
print("Error in input, try again")

except ValueError:
print("Error in input, try again")

print("Positive integers entered:", pos)


print("Negative integers entered:", neg)
print("Sum:", sum)
if neg!=0 or pos!=0:
print("Avg", sum/(neg+pos))
else:
print("Error")

3
Exercise2: Write a Python program that asks the user to enter an
integer number, and asks him with a simple question (Do you want to
continue y/n?) if he wants to enter another number. The program stops
asking the user to enter a new number when they type ″n″ as an answer
to the question. At the end, the program should display:
- The count of odd numbers.
- The sum of even numbers.
- The count of numbers ended by 5.

Sample Execution:
Please enter an integer: 3
Do you want to continue y/n : y
Please enter an integer: -5
Do you want to continue y/n : y
Please enter an integer: 6
Do you want to continue y/n : p
Wrong Answer
Do you want to continue y/n : y
Please enter an integer: a
Wrong input
Please enter an integer: 4
Do you want to continue y/n : n

count odd is 2
Sum of even is 10
count of numbers ended by 5 is 1

4
Solution:

count_odd = sum_even = count_five = 0

while True:
nb = input("Please enter an integer: ")

try:
nb = int(nb)

if nb % 2 == 0:
sum_even += nb
else:
count_odd += 1
if nb%10==5:
count_five +=1

go = input("Do you want to continue y/n")


if go =="n":
break
elif go=="y":
continue
else:
print("Wrong Answer")

except ValueError:
print("Wrong input")

print("count odd is", count_odd)


print("Sum of even is", sum_even)
print("count of numbers ended by 5 is", count_five)

You might also like