Programming Workshop - Instructions For While Loops Solved
Programming Workshop - Instructions For While Loops Solved
WHILE - FOR
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.
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.
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.
password
password
while password != key:
Enter the password:
Correct password
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
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)