Question

In: Computer Science

In this exercise we will practice using nested loops. You will write s program that prompts...

In this exercise we will practice using nested loops. You will write s program that prompts the user to enter an integer between 1 and 9 and displays a pyramid of numbers, as shown in the example below.

The program must validate the input given by the user and make sure that:

  • if the user enters a non-integer the program issues an error message and requests the user provides a valid input.
  • if the user does not enter a number between 1 and 9 the program alerts the user of the mistake and requests the user enter a valid value.
  • validation must be done using loops.

Here is a sample run:

Enter the number of lines (1 - 9): 10
Error: you must enter a value between 1 and 9. Try again.
Enter the number of lines (1 - 9): 0
Error: you must enter a value between 1 and 9. Try again.
Enter the number of lines (1 - 9): 4

   1
  212
 32123
4321234

Notes:

  • The purpose of this problem is to practice using loops and try/except.
  • Please make sure to submit a well-written program. Good identifier names, useful comments, and spacing will be some of the criteria that will be used when grading this assignment.
  • This assignment can be and must be solved using only the materials that have been discussed in class. Do not look for alternative methods that have not been covered as part of this course.

How your program will be graded:

  • correctness: the program validates input and produces the expected output: 40%
  • complies with requirements: the program properly uses nested loops, try/except): 40%
  • code style: good variable names, comments, proper indentation and spacing : 20%

Language is python 3

Solutions

Expert Solution

#Try block
try:
    #User input value
    number = int(input("Enter the number of lines (1-9): "))
    #Valditing the number, till user gives correct input.
    while number<1 or number>9:
        print("Error: you must enter a value between 1 and 9. Try again.")
        number = int(input("Enter the number of lines (1-9): "))
    #This for loop is used for counting number of rows from 1 to number
    for i in range(1,number+1):
        #Printing spaces by subtracting i from number
        for j in range(number-i):
            print(" ",end="")
        #Printing number from higher to lower from 3 to 1 or 2 to 1
        for k in range(i,0,-1):
            print(k,end="")
        #After 2 to 1 we need to print 2 and so on
        #For 1 there will be no another value
        #So, I write if condition and printing from 2 to i+1
        if i!=1:
            for k in range(2,i+1):
                print(k, end="")
        #Printing new line
        print()
#Except block
except ValueError:
    print("Enter only digits")

Sample input and output:

Enter the number of lines (1-9): 10
Error: you must enter a value between 1 and 9. Try again.
Enter the number of lines (1-9): 0
Error: you must enter a value between 1 and 9. Try again.
Enter the number of lines (1-9): 5
1
212
32123
4321234
543212345

Enter the number of lines (1-9): 10
Error: you must enter a value between 1 and 9. Try again.
Enter the number of lines (1-9): 0
Error: you must enter a value between 1 and 9. Try again.
Enter the number of lines (1-9): 4
1
212
32123
4321234


Related Solutions

Write a program In python of Jupiter notebook (using loops) that prompts the user to enter...
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter his/her favorite English saying, then counts the number of vowels in that (note that the user may type the saying using any combination of upper or lower case letters). Example: Enter your favorite English saying: Actions speak LOUDER than words. Number of wovels: 10
Write a Java program that uses nested for loops to print a multiplication table as shown...
Write a Java program that uses nested for loops to print a multiplication table as shown below.   Make sure to include the table headings and separators as shown.   The values in the body of the table should be computed using the values in the heading   e.g. row 1 column 3 is 1 times 3.
IN C++ Write a program that uses nested loops to collect data and calculate the average...
IN C++ Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the ­­program should display the...
Write a C++ program that uses nested loops to collect data and calculate the average rainfall...
Write a C++ program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number...
C++ In this lab you will be using nested for loops to print out stars in...
C++ In this lab you will be using nested for loops to print out stars in a Diamond pattern such as this: * *** ***** ******* ********* *********** ************* *************** ************* *********** ********* ******* ***** *** * For example , if number of rows=8 then the above pattern is derived. You are to take the input for the number of lines(rows) from a file named "input_diamond" and output the pattern into both the terminal and an output file named "output_diamond".
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops,...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops, if loops, while loops, arrays and conditional execution, as well as creating an image of the game board. The list below must be considered in the code. - Selecting a word from a dictionary (a text file) - Reading a single letter from the user - Building a character array showing the letters matched so far - Keeping count of the number of guessed...
Using Loops for the Hotel Occupancy calculator. You will write a program that calculates the occupancy...
Using Loops for the Hotel Occupancy calculator. You will write a program that calculates the occupancy of a hotel. Rules: 1# The hotel must have more than 2 floors and less than or equal 5 floors. 2# Each floor in the hotel can have a different number of rooms on the floor. 3# You must set the number of occupied rooms. Again, there must less rooms occupied than the number of rooms. 4# Using the total number of rooms and...
7. Average Rainfall Design a program that uses nested loops to collect data and calculate the...
7. Average Rainfall Design a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number...
Design a program that uses Nested loops to collect data and calculate the average rainfall over...
Design a program that uses Nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the numbers of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period.
Write a function posnum that prompts the user to enter a positive number and loops to...
Write a function posnum that prompts the user to enter a positive number and loops to error-check. It returns the square root of the positive number entered by the user. It calls a subfunction in the loop to print an error message. The subfunction has a persistent variable to count the number of times an error has occurred. Here is an example of calling the function: >> squarerootvalue = posnum Enter a positive number: -5 Error #1: Follow instructions! Does...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT