1)Write a program that asks the user to enter a positive integer n as the height of the following shape and prints out this shape. You must use loop to do this task. 2)Write a program that allows to...

1)Write a program that asks the user to enter a positive integer n as the height of the following shape and prints out this shape. You must use loop to do this task.

2)Write a program that allows to players to play the marble game using combinations of loop and conditional statements. In the marble game, you ask the user to enter how many marbles to start with. Then, the game begins. The first player must take 1, 2 or 3 marbles. Then the second player goes and must take 1, 2 or 3 marbles. The winner is the player who takes the last marble. Allow two users to play this game and print out the winner (player #1 or player#2). Assume that both players enter valid inputs (1, 2 or 3, and they never try to take more marbles than there are in the pile.)


Assignment 2 • Please write your code in a Jupyter Notebook file (.ipynb) and submit it on Blackboard • Make sure to label your answer of each question clearly and add comments to make it readable. • You are allowed to discuss with other students (up to three) or the instructor. Please put all the names of students that you discussed with. However individual students must write their own solutions. • Copying a program, or letting someone else copy your program, is a form of academic dishonesty . Any referred material must be cited properly. • Maximally leverage Piazza to benefit other students by your questions and answers. • Try to be updated by checking notifications in both Piazza and Blackboard class webpage. Exercise 1 (10 points) Write a program that asks the user to enter a positive integer n, and calculates the sum of the integers 1 + 3 + 5 + 7 + … + (2n-1) and prints out this sum to the screen. Your program should be able to handle the invalid case where the input is negative. Sample Run 1 Enter a positive integer: 5 The sum is: 25 Sample Run 2 Enter a positive integer: -5 The input you gave is not valid. It must be positive. Exercise 2 (20 points) Write a program that asks the user to enter a positive integer n, and prints out the following shape. You must use nested loop in your program. The gap between two numbers could be a space or a tab. Sample Run Enter a positive integer: 5 Output: 1 1 2 1 2 3 https://dos.uic.edu/community-standards/academic-integrity/ https://dos.uic.edu/community-standards/academic-integrity/ 1 2 3 4 1 2 3 4 5 Exercise 3 (20 points) In mathematics, the Leibniz formula for π, named after Gottfried Leibniz, states that ∑ (−1)? 2? + 1 ∞ ?=0 = ? 4 Which is, 1 − 1 3 + 1 5 − 1 7 +⋯ = ? 4 Write a program that calculates π. The more terms you sum up, the more accurate result you can obtain. Hints: (1) Make an infinite loop with a stopping criterion (2) If the stopping criterion satisfies, then stop the loop (3) Some simple stopping criteria: a) The number of terms you add up is a large number (e.g., 10000); b) The sum difference between the current one (with one more term) and the previous one is very small (e.g., 0.001) Exercise 4 (25 points) Marble Game Write a program that allows to players to play the marble game using combinations of loop and conditional statements. In the marble game, you ask the user to enter how many marbles to start with. Then, the game begins. The first player must take 1, 2 or 3 marbles. Then the second player goes and must take 1, 2 or 3 marbles. The winner is the player who takes the last marble. Allow two users to play this game and print out the winner (player #1 or player#2). Assume that both players enter valid inputs (1, 2 or 3, and they never try to take more marbles than there are in the pile.) Sample Run 1 How many marbles to start with? 10 Player #1, there are 10 marbles left. How many marbles will you take? 2 Player #2, there are 8 marbles left. How many marbles will you take? 2 Player #1, there are 6 marbles left. How many marbles will you take? 2 Player #2, there are 4 marbles left. How many marbles will you take? 1 Player #1, there are 3 marbles left. How many marbles will you take? 3 Player #1, you took the last marble and have won! Sample Run 2 How many marbles to start with? 10 Player #1, there are 10 marbles left. How many marbles will you take? 2 Player #2, there are 8 marbles left. How many marbles will you take? 3 Player #1, there are 5 marbles left. How many marbles will you take? 3 Player #2, there are 2 marbles left. How many marbles will you take? 2 Player #2, you took the last marble and have won! Exercise 5 (25 points) Write a program that asks the user to enter a positive integer n as the height of the following shape and prints out this shape. You must use loop to do this task. Sample Run Enter a positive integer: 5 Output: * * *** *** ***** ***** ******* ******* ******************
Sep 30, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here