.py
files on your computer, and then upload those files.This homework assignment must be submitted in Gradescope by 10am CST on Tuesday, September 14, 2021.
This homework assignment is about the topics of worksheet 3, i.e. lists, while loops and for loops.
Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.
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:
This homework assignment has 2 problems, numbered 2 and 3. The grading breakdown is:
Points | Item |
---|---|
2 | Autograder |
4 | Problem 2 |
4 | Problem 3 |
10 | Total |
Ask your instructor or TA a question by email, in office hours, or on discord.
Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.
Write a program hwk3prob2.py
that asks the user for three integers:
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.
# 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
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:
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.
# 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.
Select a username:
That username is not allowed.
Select a username: sudo
That username is not allowed.
Select a username: grogu
Your username is grogu
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
while username in prohibited_usernames or 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
print("Your username is", username)
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.