A document from MCS 260 Fall 2021, instructor Emily Dumas. You can also get the notebook file.

# MCS 260 Fall 2021 Homework 3 Solutions¶

• Course instructor: Emily Dumas
• Solutions prepared by: Johnny Joyce

## Instructions:¶

• As with homework 2, for this assignment you will upload your python code directly to gradescope, i.e. you need to create and save .py files on your computer, and then upload those files.
• This time, the grading will incorporate automated syntax checks. (Last time, the checks were done, but didn't count toward the score.)

This homework assignment must be submitted in Gradescope by 10am CST on Tuesday, September 14, 2021.

### Topic¶

This homework assignment is about the topics of worksheet 3, i.e. lists, while loops and for loops.

### Collaboration¶

Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.

### Resources you may consult¶

The course materials you may refer to for this homework are:

Expected to be most useful:

Allowed and possibly useful, but probably less relevant than the links above:

### Point distribution¶

This homework assignment has 2 problems, numbered 2 and 3. The grading breakdown is:

Points Item
4 Problem 2
4 Problem 3
10 Total

### What to do if you're stuck¶

Ask your instructor or TA a question by email, in office hours, or on discord.

## ( 1. There's no problem 1 )¶

Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.

## 2. Powers¶

Write a program hwk3prob2.py that asks the user for three integers:

• A "start value"
• A "stop value"
• An exponent, which I'll refer to as k

The program should then print the numbers n**k for each integer n between the start value and stop value (including both endpoints).

Here is an example of what the interface and output should look like. Here the user asks for the cubes of integers from 5 to 10 inclusive.

Start value: 5
Stop value: 10
Exponent: 3
125
216
343
512
729
1000

In this example, the user typed the numbers 5, 10, and 3, and the rest of the text is output from the program.

## Solution¶

In [1]:
# Get the 3 inputs from the user as integers
startvalue = int(input("Enter starting value: "))
endvalue = int(input("Enter end value: "))
k = int(input("Enter exponent (k): "))

# Start at i=startvalue and end at i=endvalue (which is why we want the +1 at the end)
for i in range(startvalue, endvalue+1):
print(i**k) # For each value of i, raise it to the exponent k and print it

Enter starting value: 5
Enter end value: 10
Enter exponent (k): 3
125
216
343
512
729
1000


## 3. Username picker¶

Write a program hwk3prob3.py that asks the user to select a username. (The name won't actually be used for anything.)

The rules for usernames are:

1. The username must not be empty.
2. There is a list of names that are not allowed (see below).

If the user requests a name that violates either rule, the program should say that name is not allowed and ask again, repeating until they enter an acceptable name. Once they enter an acceptable name, the program should just print "Your username is X" where X is replaced with whatever they entered, and exit.

In [2]:
# List of prohibited usernames.  Copy this into your code (without this comment).
prohibited_usernames = [ "system", "administrator", "ddumas", "heliotrope", "sudo", "reboot" ]


Here's what it should look like when you run the program:

Select a username: system
That username is not allowed.
That username is not allowed.
Select a username: sudo
That username is not allowed.
Select a username: grogu
Your username is grogu

## Solution¶

In [3]:
prohibited_usernames = [ "system", "administrator", "ddumas", "heliotrope", "sudo", "reboot" ]

username = input("Select a username: ") # We don't need to use str() because input() acts as a string by default

# While the username is prohibited or is a blank string. Use a while loop here so we keep asking
# the user multiple times until they give us an acceptable username
print("That username is not allowed.")
username = input("Select a username: ") # Here, we overwrite the variable called username so that
# our while loop can check if the new username is acceptable

# Once we get to this line, the while loop has stopped - therefore the username must
# be acceptable by this point, so print out the acceptable username

Select a username: system
That username is not allowed.
That username is not allowed.

It would also be acceptable for a solution to add "" to prohibited_usernames to simplify checking for validity, avoiding the need for logical or in the loop.