Question

In: Computer Science

pyramid.py program for i in range(1,6): for j in range(i): print("*",end=' ') print("\n",end='') Exercise: Create the...

pyramid.py program

for i in range(1,6):

for j in range(i):

print("*",end=' ')

print("\n",end='')

Exercise: Create the pyramid similar to the above pyramid.py program using a while loop. Name your program whilepyramid.py and submit it on blackboard.

Solutions

Expert Solution

Here is the code for the given question using while loop:

whilepyramid.py

*********************************************************************************************************************************************

i = 1 #initialising a variable i to 1 as we have to print in range 1 to 6 so start with 1
while i < 6: #continue the loop untill i is less than 6 as ending range is 6
j = 1 #initialising a variable j to 1
while j <= i: #checking if j is less than or equal to i   
print("*",end=' ') #print statement for a row
j+=1 #increment j
print("\n",end = '') #print statement for outer loop i.e change row
i+=1 #increment i

**********************************************************************************************************************************************

Providing screenshot of the code for further clarification

Output of my code :

**********************************************************************************************************************************************

For your verification i have provided your code that is by the for loop with its output:

for loop code:

pyramid.py

Output of this above code


Related Solutions

2) create a python program that uses a for loop and range to print out the...
2) create a python program that uses a for loop and range to print out the values 10 8 6 4 2 3) Create a python program that yses a for loop to print out ["bob","al","bert"]
1. Create a program that generates n values in range [low, high]. • Create a function...
1. Create a program that generates n values in range [low, high]. • Create a function generate_vals which takes as input an array of double values and three integers representing the size of the array, low value of range, and high value of range, respectively. The function should use rand() to generate the requested number of values. • Prompt the user to enter the size of their array as shown below. It cannot exceed a maximum size of 128. If...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i <n; ++ i) {cout << "j =" + j; j = j + 5; }}
def longest(string): start=0;end=1;i=0; while i<len(string): j=i+1 while j<len(string) and string[j]>string[j-1]: j+=1 if end-start<j-i: #update if current...
def longest(string): start=0;end=1;i=0; while i<len(string): j=i+1 while j<len(string) and string[j]>string[j-1]: j+=1 if end-start<j-i: #update if current string has greater length than #max start and end end=j start=i i=j; avg=0 for i in string[start:end]: avg+=int(i) print('The longest string in ascending order is',string[start:end]) print('Teh average is',avg/(end-start)) s=input('Enter a string') longest(s) i need a definition and explanation of this code that how it works?
PYTHON Let n denote an integer entered by the user. Write a program to print n...
PYTHON Let n denote an integer entered by the user. Write a program to print n multiples of 5 in the descending order, with the last number being 5. Print the average of those n multiples
Martin wants to create a program that requires the user to input 10 integers then print...
Martin wants to create a program that requires the user to input 10 integers then print the total number of even integers, highest even integer, lowest even integer, total number of odd integers, highest odd integer, and lowest odd integer. If, however, the user inputs zero (0), the program will display “You have not yet entered 10 integers.” Below is the sample output of the program Martin created. Enter the integers: Integer 1: 15 Integer 2: 9 Integer 3: 71...
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
Write a JavaFx program to create Forms as front end and JDBC as back end to...
Write a JavaFx program to create Forms as front end and JDBC as back end to store the data and retrieve the data.
In this exercise, you will create a program that displays the amount of a cable bill....
In this exercise, you will create a program that displays the amount of a cable bill. The amount is based on the type of customer, as shown in Figure 10-30. For a residential cus- tomer, the user will need to enter the number of premium channels only. For a business customer, the user will need to enter the number of connections and the number of premium channels. Use a separate void function for each customer type. If necessary, create a...
Write a Java program to generate random numbers in the following range a. 1 <=n <=...
Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 JAVA PROGRAMMING
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT