BYU logo Computer Science

To start this assignment, download this zip file.

The following guide pages cover material needed for this assignment:

Homework 3b — Input loops

For all interactive programs, be sure to include a space at the end of the string you give to input().

For example:

name = input('What is your name? ')

Notice the space between the question mark and the quotation mark.

Grading Policy:

It’s best to try to pass the tests before you turn in the assignment. Review the guide on using pytest if you need to review using pytest and what to do if a test fails.

However, while pytest checks for exact matches, the autograder gives partial credit for any partially correct solution. Even if your output doesn’t exactly match the correct output, you can still get most of the points.

1. Words

Write a program in words.py that asks a person to guess a word. The program continually prompts the person to enter a guess until they get it right. When the person enters a guess, the program checks whether it is alphabetically less than, equal to, or greater than the correct word. It has these potential responses:

  • Lower!
  • Higher!
  • You got it!

The file words.py uses 'python' as the secret word. It passes 'python' as an argument to the play() function. For a review on how arguments work, see the guide.

def play(secret_word):
    pass


if __name__ == '__main__':
    play('python')

When playing the game yourself, you can replace the word python with any word you like. However, when you turn in your assignment, make sure python is the secret word.

Note for grading: The string 'python' should only be written inside if __name__ == '__main__':, like it is in the starter code.

Here is a sample session of someone guessing a word:

Guess a word: slate
Lower!
Guess a word: crane
Higher!
Guess a word: pickles
Higher!
Guess a word: quiz
Lower!
Guess a word: poodle
Higher!
Guess a word: python
You got it!

2. Calculator

Write a program in calculator.py that provides a basic calculator. The program provides the following menu:

<empty line>
What would you like to do?
 1) Add
 2) Subtract
 3) Quit
Option:

Always print a blank line at the start of the menu.

Also note the space before each number.

If a person enters 1, then prompt for two numbers:

Number 1: 5
Number 2: 6

and print out the result of adding them.

If a person enters 2, then prompt for two numbers:

Number 1: 6
Number 2: 17

and print out the result of subtracting them.

If a person enters 3, then quit the program.

If a person enters something other than these three numbers, print:

Unrecognized response: [response]

The file calculator.py has some starting code:

def calculator():
    pass


if __name__ == '__main__':
    calculator()

Here is a sample session of someone interacting with the calculator:


What would you like to do?
 1) Add
 2) Subtract
 3) Quit
Option: 2
Number 1: 16
Number 2: 7
9

What would you like to do?
 1) Add
 2) Subtract
 3) Quit
Option: 1
Number 1: 91
Number 2: 22
113

What would you like to do?
 1) Add
 2) Subtract
 3) Quit
Option: help
Unrecognized response: help

What would you like to do?
 1) Add
 2) Subtract
 3) Quit
Option: 3

Tests

Be sure you can pass the tests before you turn in the assignment. Review the guide on using pytest if you need to review using pytest and what to do if a test fails.

Grading

ActivityPoints
Words10
Calculator10

Manual grading will focus on decomposition and fluency.