Mario

screenshot of Mario jumping up pyramid

Implement a program that prints out a half-pyramid of a specified height, per the below.

$ ./mario
Height: 4
   #
  ##
 ###
####

Accepting this Assignment

  1. Accept this assignment via GitHub Classroom.
  2. After about a minute, refresh the page and ensure you see “You’re ready to go!”.

Getting Started

Log into code.cs50.io, click on your terminal window, and execute cd by itself. You should find that your terminal window’s prompt resembles the below:

$

Next execute

get50 sentimental-mario-less

in order to download a directory called sentimental-mario-less into your codespace.

Then execute

cd sentimental-mario-less

in order to change into that directory. Your prompt should now resemble the below:

sentimental-mario-less/ $

Execute ls by itself, and you should see a few files and folders:

mario.py

If you run into any trouble, follow these same steps again and see if you can determine where you went wrong!

Specification

  • Write, in a file called mario.py, a program that recreates the half-pyramid using hashes (#) for blocks, exactly as you did in Problem Set 1, except that your program this time should be written in Python.
  • To make things more interesting, first prompt the user with get_int for the half-pyramid’s height, a positive integer between 1 and 8, inclusive.
  • If the user fails to provide a positive integer no greater than 8, you should re-prompt for the same again.
  • Then, generate (with the help of print and one or more loops) the desired half-pyramid.
  • Take care to align the bottom-left corner of your half-pyramid with the left-hand edge of your terminal window.

Usage

Your program should behave per the example below.

$ ./mario
Height: 4
   #
  ##
 ###
####

Testing

While check50 is available for this problem, you’re encouraged to first test your code on your own for each of the following.

  • Run your program as python mario.py and wait for a prompt for input. Type in -1 and press enter. Your program should reject this input as invalid, as by re-prompting the user to type in another number.
  • Run your program as python mario.py and wait for a prompt for input. Type in 0 and press enter. Your program should reject this input as invalid, as by re-prompting the user to type in another number.
  • Run your program as python mario.py and wait for a prompt for input. Type in 1 and press enter. Your program should generate the below output. Be sure that the pyramid is aligned to the bottom-left corner of your terminal, and that there are no extra spaces at the end of each line.
#
  • Run your program as python mario.py and wait for a prompt for input. Type in 2 and press enter. Your program should generate the below output. Be sure that the pyramid is aligned to the bottom-left corner of your terminal, and that there are no extra spaces at the end of each line.
 #
##
  • Run your program as python mario.py and wait for a prompt for input. Type in 8 and press enter. Your program should generate the below output. Be sure that the pyramid is aligned to the bottom-left corner of your terminal, and that there are no extra spaces at the end of each line.
       #
      ##
     ###
    ####
   #####
  ######
 #######
########
  • Run your program as python mario.py and wait for a prompt for input. Type in 9 and press enter. Your program should reject this input as invalid, as by re-prompting the user to type in another number. Then, type in 2 and press enter. Your program should generate the below output. Be sure that the pyramid is aligned to the bottom-left corner of your terminal, and that there are no extra spaces at the end of each line.
 #
##
  • Run your program as python mario.py and wait for a prompt for input. Type in foo and press enter. Your program should reject this input as invalid, as by re-prompting the user to type in another number.
  • Run your program as python mario.py and wait for a prompt for input. Do not type anything, and press enter. Your program should reject this input as invalid, as by re-prompting the user to type in another number.

Execute the below to evaluate the correctness of your code using check50. But be sure to compile and test it yourself as well!

check50 cs50/problems/2021/fall/sentimental/mario/less

Execute the below to evaluate the style of your code using style50.

style50 mario.py

This problem will be graded only along the axes of correctness and style.

How to Submit

In your terminal window, execute the below.

submit50 sentimental-mario-less

You may submit as many times as you would like up until the deadline. To confirm your submission, go to https://github.com/classroom50/sentimental-mario-less-USERNAME, where USERNAME is your GitHub username. You should see the code from your latest submission.