EX03 - while-loop


Exercise 00 – Factorial! (!!!)

  1. In the comp110/exercises directory create a new folder named ex03_while_loop
  2. Create a file named factorial.py in your comp110/exercises/ex03_while_loop folder.
  3. Use the input and int built-in functions to prompt the user for 1 integer.
  4. Compute the factorial of the number and print this out.
    1. Note: you may NOT use the built-in factorial function for this calculation.
    2. We will only test your function with positive integers. No need to worry about computing the factorial of a negative number!

Example program run:

Choose a number: 4
Factorial: 24

Exercise 01 - Repeat that Beat

This program will repeat a chosen word or phrase a specified number of times.

  1. Create a file named repeat_beat.py in your comp110/exercises/ex03_while_loop folder.
  2. Use the input function to prompt the user for a beat (whatever string they want to repeat) and a number (however many times they want to repeat the beat).
  3. The printed output should be the beat repeated however many times the user specified, with a space between each instance of the beat.
  4. If the number given by the user is less than or equal to 0, print “No beat…”.

Example program run A:

What beat do you want to repeat? bop
How many times do you want to repeat it? 3
bop bop bop 

Example program run B:

What beat do you want to repeat? boom
How many times do you want to repeat it? -1
No beat...

Notes:

  • For full credit, you shouldn’t have a space after the final beat. For example, repeating bop 3 times should not print "bop bop bop ".
  • If you’ve encountered the string multiplication operator before, we don’t want you to use it for this assignment. Use the concatenation operator instead for now.
  • Consider having a str variable, separate from the beat, that you can add to in a repetitive fashion.

Exercise 02 – Primes!

  1. In the comp110/exercises/ex03_while_loop directory create a program named is_prime.py
  2. Use the input and int built-in functions to prompt the user for 1 integer.
  3. Print all of the number’s factors in increasing order and determine if it is prime. Reminder: a number is prime if its only factors are 1 and itself.
    1. If the number is prime, print <num> is prime!
    2. If the number is not prime, print <num> is not prime.
    3. Hint: The modulo operator – % – will be key here. Any number modulo a factor of itself with be 0. Ex: 4 % 2 evaluates to 0.

Example program run A:

Choose a number: 5
1
5
5 is prime.

Example program run B:

Choose a number: 8
1
2
4
8
8 is not prime.

Formatting and Documentation

  1. You should have a module docstring with a complete first sentence describing your progam at the start of each exercise.
  2. In each of your files, initialize a global variable named __author__ set equal to your PID as a string

Submission

Go ahead and delete any submission zips lingering around in your workspace from the previous exercise.

When you are ready to submit for grading, close out any open Python Debug Console terminals using the Trash Can and then open up a clean, new terminal.

python -m tools.submission comp110/exercises/ex03_while_loop

This should produce a submission timestamped with the current date/time for uploading on Gradescope.

Make a Backup Commit

  1. Open the Source Control panel (Command Palette: “Show SCM” or click the icon with three circles and lines on the activity panel).
  2. Notice the files listed under Changes. These are files you’ve made modifications to since your last backup.
  3. Move your mouse’s cursor over the word Changes and notice the + symbol that appears. Click that plus symbol to add all changes to the next backup. You will now see the files listed under “Staged Changes”.
    • If you do not want to backup all changed files, you can select them individually. For this course you’re encouraged to back everything up.
  4. In the Message box, give a brief description of what you’ve changed and are backing up. This will help you find a specific backup (called a “commit”) if needed. In this case a message such as, “Finished Exercise 0!!” will suffice.
  5. Press the Check icon to make a Commit (a version) of your work.
  6. Finally, press the Ellipses icon (…) and select “Push” to send this backed up version to your workspace repository space on GitHub.