Python loops programs| For, While, Nested loops
Python Programs 


Learn comlex python loops programs that are must to understand python programming language using for loop and while loop and how to append with for and while loop.

These are the simple python loops programs for practice.We have listed four  python loops programs and their solutions in which for and while loop are used.

Python loops programs and answers:

Python For loop program:

Q- Write a program that asks for the number of participants of the event and takes the number of participants times the amount that each participant paid. Calculate the total amount collected from participants.

Solution:

# a=int(input("enter number of participants "))
# b=0
# for i in range(a):
# c= int(input("enter the amount you paid"))
# b=b+c
# print("your total amount is",b)
Python loops programs| For, While, Nested loops
Python for loop



While loop in python:

Q: Re-write or update the above program. Now the event pass has the cost of 1200 PKR. the program should take the amount paid by participants which could be 1200 or less. Calculate the total amount paid by participants and the total discount given to participants.

Solution:

a=int(input("enter number of participants "))
b=0
x=0
for i in range(a):
    c=int(input ("enter the amount you paid"))
    while c>1200:
        c=int(input ("amount must be less than or equals to 1200 !! Try again"))
        p=1200-c
        b=b+c
x=x+p
print("your total amount is",b)
print("your total discount is",x)
Python loops programs| For, While, Nested loops
Python while loop 



Append with for loop in python:

Q- Write a program for an open event, the ten (10) people participanting in the program, and the event hall has a maximum of hundred (100) participants capacity. Anyone else who walked in could also join the event by paying the event fee. The program should ask for the participant's name and the fee s/he paid. After collecting participant information it should ask whether registration is still open or not. If the registration desk is closed, the program should return the list of participants with the amount they have paid and the total amount collected. 

Solution:

Desk = 'Open'
Particients = int(input("Enter the number of participents 10-100: "))
while Particients<10 or Particients>100:
    Particients = int(input("Enter the number of participents 10-100: "))
lst = []
lst1 = []
while Desk == 'Open':
    lst.append(input("Enter the name of the participent: "))
    lst1.append(int(input("Enter the amount he/she paid: ")))
    if len(lst) == Particients:
        Desk = 'Closed'
        print("Desk Closed")
                                       for i in range(len(lst)):
            print(lst[i] + ' paid ' + str(lst1[i]))
        print('The total amount collected is: ' + str(sum(lst1)))
Python loops programs| For, While, Nested loops


Append with while loop in python:

Q: Re-write or update the Q3 program. Now the
event pass has the cost of 1200 PKR. the program should take the amount
paid by participants which could be 1200 or less, but not less than the 700PKR. The program should return the list of participants with the amount they have paid and a discount is given to
each participant. Then show the total no. of participants, the total amount paid by participants, and the total discount given to participants.

Solution:

Desk = 'Open'
lst = []
lst1 = []
discount = 0

Particients = int(input("Enter the number of participents 10-100: "))
'''while Particients<10 or Particients>100:
    Particients = int(input("Enter the number of participents 10-100: "))'''

while Desk == 'Open':
    lst.append(input("Enter the name of the participent: "))
    amount = int(input("Enter the amount you want to pay: "))
       while amount<700 or amount>1200:
        if amount<700:
            amount = int(input("You can not pay less than 700 enter the amount again: "))
        elif amount>1200:
            amount = int(input("You can not pay more than 1200 enter the amount again: "))
    lst1.append(amount)
    if len(lst) == Particients:
        Desk = 'Closed'
        print("Desk has been Closed\n")
        for i in range(len(lst)):
            print(i+1, lst[i] + ' paid ' + str(lst1[i]) + ' and the discount given is: ' + str(round(((1200-lst1[i])/1200)*100)) + ' %')
        print('\nThe total amount collected is: ' + str(sum(lst1)))
        print('The total discount given to the participents is: ' + str(round((((1200*Particients)-sum(lst1))/(1200*Particients))*100)) + ' %')
Python loops programs| For, While, Nested loops



Online python compilers: