Here are some optional practice problems to help you learn loops. You can implement all of them
using either for
or while
loops - or try both!
Write a program that asks the user to enter a positive integer. It should continue to prompt the user until they enter a positive integer, at which point, it should print the number the entered.
Example Output:
Enter a positive integer: 0
Enter a positive integer: -3
Enter a positive integer: 5
You entered 5
Modify the program from Part A so that it informs the user of the problem if their number is invalid.
Example Output
Enter a positive integer: 0
0 is not a positive integer
Enter a positive integer: -3
-3 is not a positive integer
Enter a positive integer: 5
You entered 5
Modify the program from Part B so that user gets 5 attempts to print number. After the 5th attempt the program should inform the user they are out of tries and exit.
Example Output (success):
Enter a positive integer: 0
0 is not a positive integer
Enter a positive integer: -3
-3 is not a positive integer
Enter a positive integer: 5
You entered 5
Example Output (failure):
Enter a positive integer: 0
0 is not a positive integer
Enter a positive integer: -3
-3 is not a positive integer
Enter a positive integer: -1
-1 in not a positive integer
Enter a positive integer: -1
-1 in not a positive integer
Enter a positive integer: -1
-1 in not a positive integer
You ran out of tries
Modify the program from Part C so that each time it prompts the user it informs them how many tries they have remaining.
Example Output:
Enter a positive integer (5 attempts left): 0
0 is not a positive integer
Enter a positive integer (4 attempts left): -3
-3 is not a positive integer
Enter a positive integer (3 attempts left): 5
You entered 5
Modify the program from Part D so that the user must enter an even number. Make sure you modify the prompt so that it informs the user of this additional requirement. Also, make sure you inform the user if the number is not even.
Enter a positive, even integer (5 attempts left): -2
-2 is not a positive integer
Enter a positive, even integer (4 attempts left): 5
5 is not even
Enter a positive, even integer (3 attempts left): -3
-3 is not a positive integer
-3 is not even
Enter a positive, even integer (2 attempts left): 6
You entered 6
Write a program that prompts the user repeatedly for some number of integers. Then print the maximum value that the user entered. The program should prompt the user first for the number of integers they would like to enter (must be at least 2). As it prompts the user it should inform them of how many numbers they have left to enter.
Example output:
How many numbers would you like enter? 0
You must enter at least 2 numbers.
How many numbers would you like enter? 4
Enter an integer (4 remaining): 6
Enter an integer (3 remaining): 9
Enter an integer (2 remaining): 1
Enter an integer (1 remaining): 5
The maximum value is 9
Modify your program from Part A so that it also prints the average of the values entered.
Example output:
How many numbers would you like enter? 0
You must enter at least 2 numbers.
How many numbers would you like enter? 4
Enter an integer (4 remaining): 6
Enter an integer (3 remaining): 9
Enter an integer (2 remaining): 1
Enter an integer (1 remaining): 5
The maximum value is 9
The average value is 5.25
Write a program that prompts the user to enter a positve integer. Then print a number of
rows equal to that number. On the first row, print a single *
. On each subsequent row, print two
more *
s than the previous row.
Example output:
Enter a positive integer: 0
0 is not a positive integer.
Enter a positive integer: 5
*
***
*****
*******
*********
Modify your program from Part A so that it centers each row on the row below it:
Example output:
Enter a positive integer: 0
0 is not a positive integer.
Enter a positive integer: 5
*
***
*****
*******
*********
Modify your program from Part B so that it prompts the user for a number greater than 2. Then print a tree shape by printing the same triangle as before, but adding a trunk. The trunk should be centered and its width and height should be a constant proportion of the tree's width and height (the proportion is up to you).
Example output:
Enter an integer greater than 2: 1
1 is not greater than 2.
Enter an integer greater than 2: 5
*
***
*****
*******
*********
***
***
Enter an integer greater than 2: 9
*
***
*****
*******
*********
***********
*************
***************
*****************
*****
*****
*****