Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views6 pages

Programming Workshop - Instructions For While Loops Solved

The document presents 24 exercises that involve the use of while and for loops in Python. The exercises ask to develop programs that request data from the user, perform calculations and print results using these types of nested and/or simple loops. The exercises cover topics such as the sum of numbers, averages, prime numbers, sorting, among others.
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)
11 views6 pages

Programming Workshop - Instructions For While Loops Solved

The document presents 24 exercises that involve the use of while and for loops in Python. The exercises ask to develop programs that request data from the user, perform calculations and print results using these types of nested and/or simple loops. The exercises cover topics such as the sum of numbers, averages, prime numbers, sorting, among others.
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
You are on page 1/ 6

WORKSHOP INSTRUCTIONS FOR CYCLIC FLOW

WHILE - FOR

1. Read an integer n, print the even numbers from 0 to n. Use the


instruction for

2. Read an integer n, print the odd numbers from 0 to n. Utilize the


instruction for
3. Read two integer numbers n and m, print the numbers from 0 to n and their respective
number raised to the m.

4. Develop a program that asks the user for numbers within a while loop.
integers and obtain the minimum of the entered values. At the end of the cycle, print the
lowest value.

5. Develop a program in which the user is asked for numbers within a for loop.
enter integers and indicate on screen if the entered elements are sorted from smallest to
mayor or not.

6. Develop a program that asks the user for numbers in a while loop
Enter integers and display the average of all the entered elements.

7. Develop a program that asks the user in a while loop for integer numbers and
indicate on screen how many of those elements are odd numbers.

8.Write a program that asks how many numbers are going to be


introduce, ask for those numbers and write how many negatives there are
introduced.

9.Write a program that asks for an integer greater than 1 and


whether it is a prime number or not.

10.Write a program that asks for two integers and writes them
sum of all integers from the first number to the
second.

11. Write a program that asks the user for a word and displays it on the screen 10
times.

word = input('Enter a word: ')


for i in range(10):
print(word)
12. Write a program that asks the user for their age and displays on screen all the
years that he/she has turned (from 1 to his/her age).

age = int(input('How old are you? '))


for i in range(age):
You have turned

13.Write a program that asks the user for an amount to invest, the interest
annual and the number of years, and display on screen the capital obtained in the
investment every year that the investment lasts.

amount = float(input('Amount to invest? '))


float(input("Annual percentage interest? "))
years = int(input("Years?"))
for i in range(years):
amount *= 1 + interest / 100
Capital after

14. Write a program that stores the string of characterspasswordin a


variable, ask the user for the password until they enter it
correct password.

password
password
while password != key:
Enter the password:
Correct password

15. Write a program that echoes everything the user inputs


until the user types 'exit' which will end.

while True:
Enter something:
if phrase == "exit":
break
print(phrase)

16.Read integers from the keyboard until the user enters 0. Finally,
show the sum of all the entered numbers.
Option 1

total=0
nro=int(input("Number: "))
while nro != 0:
total = total + nro
nro=int(input("Number: "))
print("The sum of the numbers is: ", total)
Option2

total=0
nro=int(input("Number: "))
while nro != 0:
total += nro
number=int(input("Number: "))
print("The sum of the numbers is: ", total)

17. Read integers from the keyboard until the user enters 0. Finally,
show the sum of all the positive numbers entered.

positivos=0
n=int(input("Number (0 to finish): "))
while n != 0:
if n>0:
positives += 1
number (0 to finish):
print("Number of positives:", positives)

18. Read positive integers from the keyboard until the user enters 0. Report
what was the highest number entered.

mayor=-1
n = int(input("Positive number:"))
while n >= 0:
if n > mayor:
n
n=int(input("Positive number:"))
print("Highest number entered:", highest)

19.Read a positive integer from the keyboard and print the sum of its digits.
what it consists of

suma=0
n=int(input("Positive number:"))
while n != 0:
digit=n%10
sum += digit
n = n // 10
print("Sum of the digits:", sum)

20.Request the user to enter positive integers and, for each one,
print the sum of the digits that compose it. The stopping condition is that
input the number -1. At the end, show how many of the entered numbers
For the user, they were even numbers.
pares=0
n=int(input("Number (-1 to end the program): "))
while n!=-1:
if n%2 == 0:
pairs += 1
sum=0
while n!=0:
digit = n % 10
sum += digit
n = n // 10
Sum of its digits:
n=int(input("Number (-1 to end the program): "))
Entered

21.Show a menu with three options: 1- start program, 2- print list,


3-finalize program. Next, the user must be able to select a
option (1, 2 or 3). If you choose an incorrect option, inform you of the error. The menu is
It must be displayed again after executing each option, allowing to go back.
choose. If you select options 1 or 2, a text will be printed. If you select option 3, it will
It will interrupt the printing of the menu and the program will finalize.

while True:
Option 1 - start program
Option 2 - print list
Option 3 - end program
option=int(input("Chosen option: "))
if option==1:
Let's get started!
elif option==2:
List:
Nadia, Esteban, Mariela, Fernanda
elif option == 3:
Until next time
break
else:
Incorrect option. You must enter 1, 2, or 3

22. Create a program that allows the user to enter the amounts of their purchases.
client (the amount of data to be uploaded is unknown, which may change each time
execution), cutting off data entry when the user inputs the amount 0.
If a negative amount is entered, it should not be processed and the user should be asked to enter a
new amount. Upon completion, inform the total to be paid taking into account that, if the
Sales exceeding a total of $1000 are subject to a 10% discount.

total=0
amount=float(input("Amount of a sale: $"))
while monto != 0:
if amount < 0:
Invalid amount.
else:
total += amount
amount=float(input("Amount of a sale: $"))
if total > 1000:
total -= total * 0.1
Total amount to pay: $

23.Create a program that requests the input of positive whole numbers, until
that the user enters 0. For each number, inform how many even digits and
how many odd numbers it has.
At the end, report the number of even digits and odd digits read.
total.

number:
totalPares=0
totalImpares=0
while number != 0:
pares=0
impares=0
while number != 0:
lastdigit=number%10
if lastdigit % 2 == 0:
pairs += 1
totalPairs += 1
else:
odds += 1
totalOdds+=1
number=number//10
The entered number has
odd digits
number=int(input("Another number: "))
print("Total of even digits:", totalPares)
print("Total of odd digits:", totalImpares)

24. Write a program that requests the input of an indeterminate amount of


numbers greater than 1, ending when a zero is received. Print the
number of prime numbers entered.
cantidad=0
n=int(input("Number: "))
while n!=0:
primo=True
for i in range(2,n):
if n % i == 0:
primo=False
break
if first:
quantity += 1
n=int(input("Number: "))
print("primes: ", count)

You might also like