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

MCS 260 Fall 2021 Homework 2 Solutions

  • Course instructor: David Dumas
  • Solutions prepared by: Kylash Viswanathan and David Dumas

Instructions:

This is the first homework where you'll write Python code, so the instructions are detailed and long. The actual problems are brief.

Deadline

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

Topic

This homework assignment is about the topics of worksheet 2, i.e. variables, the built-in types int, float, and string, and the use of booleans and conditionals.

Collaboration

No collaboration is permitted, 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 not the most relevant:

Point distribution

This homework assignment has 2 problems, numbered #2 and #3. Each problem is worth 4 points.

Because of the way Gradescope works, there will also be a problem 1 worth 0 points.

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

In Gradescope, problem 1 is used to report the result of autograder testing. In this homework, the autograder will ONLY give you advice (warn you of syntax issues etc.) but it won't actually assign any points. Everyone will get a full score (0 out of 0) on problem 1.

2. Bumblebee detector

Write a program hwk2prob2.py that waits for the user to type one line of text.

If the user types Bumblebee (capital B) followed by the Enter key, the program should print BUMBLEBEE DETECTED. Otherwise, it should print NO BUMBLEBEE.

Sample usage (first line is always what the user typed):

  • Bumblebee
    BUMBLEBEE DETECTED
  • Feral raccoon holding an improvised spear
    NO BUMBLEBEE
  • This line of text has Bumblebee in it but is not just that word by itself
    NO BUMBLEBEE

Solution

In [ ]:
# Contents of hwk2prob2.py
input_word = input()
if input_word == "Bumblebee":
    print("BUMBLEBEE DETECTED")
else:
    print("NO BUMBLEBEE")

3. Exponent calculator

Write a program hwk2prob3.py that asks the user for a base $b$ and exponent $x$ (both of them floats) and which then prints the value of $b^x$ (i.e. $b$ raised to the power $x$). You don't need to do any checking to make sure the input is valid.

The interface should look like this (two examples shown):

  • Base: 2.5
    Exponent: 8
    1525.87890625
    (Here, 2.5 and 8 are values typed by the user.)
  • Base: 64
    Exponent: 0.5
    8.0
    (Here, 64 and 0.5 are values typed by the user.)

Solution

In [2]:
# Contents of hwk2prob3.py
base = float(input("Base: "))
exp = float(input("Exponent: "))
print(base**exp)
Base: 2
Exponent: 5
32.0

Revision history

  • 2021-09-14 Initial publication