BYU logo Computer Science

To start this assignment, download this zip file.

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.

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.

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 contains this sample code:

def play(secret_word):
    pass


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

You are welcome to replace the word python with any word you like. However, when you turn in your assignment, please make sure python is the secret word.

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:


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.