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...
Exercise 3 – Strings Using a function Write a program that prompts the user to enter...
Exercise 3 – Strings Using a function Write a program that prompts the user to enter two inputs: some text and a word. The program outputs the starting indices of all occurrences of the word in the text. If the word is not found, the program should output “not found”. Example1: Input1: my dog and myself are going to my friend Input2: my Output: 0 11 31 Example 2: Input1: Programming is fun Input 2: my Output: not found
Exercise 4 – Lists and input Using a function Write a program that prompts the user...
Exercise 4 – Lists and input Using a function Write a program that prompts the user to input a sequence of words. The program then displays a list of unique words (words that only occurred once, i.e. no repeated).
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...
Using Matlab: Using nested for loops (other methods are not allowed), write a function which takes...
Using Matlab: Using nested for loops (other methods are not allowed), write a function which takes as input a matrix, and as output returns true if at least one entry in the matrix is strictly greater than 8 and returns false otherwise.
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...
MATLAB is program Respond fast please a quiz Create a MATLAB script. Using nested for loops,...
MATLAB is program Respond fast please a quiz Create a MATLAB script. Using nested for loops, evaluate the multivariable function: z = sin ⁡ ( x ) cos ⁡ ( y ) for x between -4 and 4 in steps of 1 y between -2 and 2 in steps of 1 Display the matrix z Cut and paste the following into a word doc and submit to this question. Your script code Your input Your output
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT