Nov-15-2018, 11:49 PM
I’m trying to write a guessing game where the computer chooses a number between 1 and 100 and it’s up to the user to guess what the number is. It’s part of a Udemy course I am taking.
The task is for practicing using while loops.
Here the challenge directly from the course instructor's Jupyter Notebook:
Here are the problems with my code (in order from least significant to most significant):
The task is for practicing using while loops.
Here the challenge directly from the course instructor's Jupyter Notebook:
Quote:Write a program that picks a random integer from 1 to 100, and has players guess the number. The rules are:I can scratch off #1 and #4 because I have effectively implemented them. I am struggling with #2 and #3. Before we address #2 and #3, let’s address the ways in which my code is already problematic. Here is my code:
- If a player's guess is less than 1 or greater than 100, say "OUT OF BOUNDS"
- On a player's first turn, if their guess is
- within 10 of the number, return "WARM!"
- further than 10 away from the number, return "COLD!"
- On all subsequent turns, if a guess is
- closer to the number than the previous guess return "WARMER!"
- farther from the number than the previous guess, return "COLDER!"
- When the player's guess equals the number, tell them they've guessed correctly and how many guesses it took!
import random print("Here are the rules: /nGuess a number between 1 and 100. We'll give you a hint if you need one.") num = int(random.randint(0,101)) print(num) guess_list = int(1) guess = int(input("Guess a number! ")) while guess != num: guess_list = guess_list + 1 if guess <= 0: print("Out of Bounds. Try again.") if guess > 100: print("Out of Bounds. Try again.") guess = int(input("Guess again! ")) if guess == num: print("You've won! It took you %d tries, but you got it. " % (guess_list))Here is how the script runs in my Jupyter Notebook: https://i.imgur.com/yJvKsk2.png
Here are the problems with my code (in order from least significant to most significant):
- I add a new line character to the first string. But when the rules are actually printed in the output, a new line is not entered, why? What is wrong with the way I am trying to add a new line?
- The first line of output should reflect the order in which operations are declared in the script. For example line 2 prints the rules, line 4 prints the numbers and line 6 prompts the user for an integer. So these operations should transpire in that order. But instead, in the output first the user is prompted. Why is the user prompted to enter a number when that shouldn’t happen until after the rules are printed and the random integer is generated?
- Notice the “In [ * ]”? That indicates that the script is stuck in a loop. Usually when this happens, my Jupyter Notebook is frozen however in my case the script continues to run without an issue until I interrupt the kernel, which then produces the following traceback. What is this ugly traceback trying to say?
--------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) /usr/lib/python3.7/site-packages/ipykernel/kernelbase.py in _input_request(self, prompt, ident, parent, password) 728 try: --> 729 ident, reply = self.session.recv(self.stdin_socket, 0) 730 except Exception: /usr/lib/python3.7/site-packages/jupyter_client/session.py in recv(self, socket, mode, content, copy) 802 try: --> 803 msg_list = socket.recv_multipart(mode, copy=copy) 804 except zmq.ZMQError as e: /usr/lib/python3.7/site-packages/zmq/sugar/socket.py in recv_multipart(self, flags, copy, track) 466 """ --> 467 parts = [self.recv(flags, copy=copy, track=track)] 468 # have first part already, only loop while more to receive zmq/backend/cython/socket.pyx in zmq.backend.cython.socket.Socket.recv() zmq/backend/cython/socket.pyx in zmq.backend.cython.socket.Socket.recv() zmq/backend/cython/socket.pyx in zmq.backend.cython.socket._recv_copy() /usr/lib/python3.7/site-packages/zmq/backend/cython/checkrc.pxd in zmq.backend.cython.checkrc._check_rc() KeyboardInterrupt: During handling of the above exception, another exception occurred: KeyboardInterrupt Traceback (most recent call last) <ipython-input-2-c26f06c026cc> in <module>() 4 print(num) 5 guess_list = int(1) ----> 6 guess = int(input("Guess a number! ")) 7 while guess != num: 8 guess_list = guess_list + 1 /usr/lib/python3.7/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt) 702 self._parent_ident, 703 self._parent_header, --> 704 password=False, 705 ) 706 /usr/lib/python3.7/site-packages/ipykernel/kernelbase.py in _input_request(self, prompt, ident, parent, password) 732 except KeyboardInterrupt: 733 # re-raise KeyboardInterrupt, to truncate traceback --> 734 raise KeyboardInterrupt 735 else: 736 break KeyboardInterrupt:For my future reference, the course material I am working with in this thread can be found on the teacher’s GitHub page (Jose Portilla). Here is the repo’s top level directory. The module I am working on is 02-Python Statements > 09-Guessing Game Challenge.ipynb. Here is my progress tracked on my forked repository.