This assignment focuses on loops, numbers, and lists.
You may use for loops and/or while loops.
Write all of your code in a file named guessing.py
.
Your program allows the user to play a guessing-game in which two “players” participate.
Player 1 has a very simple job: all he/she need do is enter a number between 0 and 100 for player 2 to guess. If the number entered is negative, or greater than 100, your program will complain (see the example outputs).
Next, Player 2 must repeatedly guess numbers until he/she finds the one that player 1 chose. After each incorrect guess, you will tell the user whether the correct answer is higher or lower. Once the correct number is guessed, you will inform player 2 of this, and report how many guesses it took them, and show all of the numbers that they guessed, in order.
Your program must exactly reproduce the format and behavior of the logs in this document, and on the diff-testing tool. Below are a few example runs of the program:
Welcome to the guessing game!
Player 1: Enter number for Player 2 to guess between 0 and 100: 78
Player 2, guess a number: 50
Too low...
Player 2, guess a number: 90
Too high...
Player 2, guess a number: 70
Too low...
Player 2, guess a number: 80
Too high...
Player 2, guess a number: 75
Too low...
Player 2, guess a number: 77
Too low...
Player 2, guess a number: 78
Correct!
It took you 7 tries to guess correctly!
The numbers you guessed were: 50 90 70 80 75 77 78
Goodbye!
Welcome to the guessing game!
Player 1: Enter number for Player 2 to guess between 0 and 100: 12
Player 2, guess a number: 30
Too high...
Player 2, guess a number: 20
Too high...
Player 2, guess a number: 10
Too low...
Player 2, guess a number: 12
Correct!
It took you 4 tries to guess correctly!
The numbers you guessed were: 30 20 10 12
Goodbye!
Your output will differ depending on the number entered by player 1 and the numbers guessed by player 2. However, the overall output structure should match that shown in the examples.
First, the program prints out a basic introduction.
Next, a guessing game is played. Player 1 enters the number to be guessed. Then, the game asks player 2 for guesses until the correct number is guessed. After each incorrect guess, the program gives a clue about whether the correct number is higher or lower than the guess. Once the user types the correct number, the game ends and the program reports how many guesses were needed, and what all of the guessed were. At this point, the program will print a goodbye message and then exit.
You should handle the special case where player 2 guesses the correct number on the very first try. Print a message as follows:
Player 2, guess a number: 30
You got it right in 1 guess! Wow, you are AMAZING.
You may assume valid user input, except you must validate the number entered by player 1. When prompted for numbers, player 2 will type integers only, and they will be in proper ranges.
Define a variable that establishes the maximum number that player 1 can input for player 2 to guess.
This should be 100 by default, bit should be something that is changeable.
Don’t use 100
directly in your code.
Instead, declare a variable at the very top of your program 100
, and use the variable to check the limit.
The example output in this spec shows games from 1 to 100, but you should be able to change the variable value to use other ranges such as from 1 to 50 or any maximum.
Use your variable throughout your code and do not refer to the number 100 directly.
Test your program by changing your variable and running it again to make sure that everything uses the new value.
The web site shows other expected output cases.
Read user input using input()
.
Produce repetition using while or for loops. Some students try to avoid properly using while loops by writing a function that calls itself, or a pair of functions A and B where A calls B and B calls A, creating a cycle of calls. Such solutions are not appropriate on this assignment and will result in a deduction.
For this assignment you are limited to the language features discussed in lecture so far.
Structure your solution using functions that accept parameters and return values where appropriate. For full credit, you must have at least the following two functions other than main in your program:
You may define more functions if you like. It is okay for some print statements to be in main, as long as you use good structure and main is concise.
Use whitespace and indentation properly. Limit lines to 100 characters. Give meaningful names to functions/variables, and follow Python’s naming standards. Localize variables. Put descriptive comments at the start of your program and each function. Since this program has longer functions, also put brief comments inside functions on complex sections of code.
This is due on 7/10/2017 at 7:00pm.
You should submit a single python file named guessing.py
, with each of the functions in it.
Submit these files to the associated D2L dropbox.
Following these turn-in and naming instructions closely is very important, because our grading scripts will depend on some of the details.
You may lose points if these instructions are not followed precisely!
Based on an assignment by Marty Stepp, Stuart Reges, and Allison Obourn