Skip to content

Instantly share code, notes, and snippets.

@djjr
Created October 4, 2022 17:06
Show Gist options
  • Save djjr/c42dd64786bd06609b615f107bd7fc4a to your computer and use it in GitHub Desktop.
Save djjr/c42dd64786bd06609b615f107bd7fc4a to your computer and use it in GitHub Desktop.
EB-CB03-08-BO-01.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyM3O1pggZ4F6pwAem6WJoIL",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/djjr/c42dd64786bd06609b615f107bd7fc4a/eb-cb03-08-bo-01.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"### Hi-Lo Game\n",
"\n",
"In our simple implementation of the Hi-Lo game, the program should generate a number between 0 and 100 at random, ask the user for their guess and check whether they guess correctly.\n",
"\n",
"#### Exercise 1\n",
"\n",
"To build the game, your program needs to \"choose\" a number. But if there's one thing we know, it is that when we write Python code, it will do the same thing each time we run it. So how can we get the program to choose a new number each time? Luckily, Python has a module available that will let us easily generate random numbers.\n",
"\n",
"To use the random number generation functionality of Python you need to: \n",
"\n",
"1. import the random module, and\n",
"1. call a function in the random module that selects a random number for you. \n",
"\n",
"\n",
"Try out the code cells below to see how it works. "
],
"metadata": {
"id": "b-CS505kyfqi"
}
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "FeEnnuTCyM88",
"outputId": "84702a41-63d6-4b8a-9bc7-b98c1d0d0b66"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"3\n"
]
}
],
"source": [
"import random\n",
"print(random.randrange(1,8))"
]
},
{
"cell_type": "code",
"source": [
"import random\n",
"\n",
"for i in range(20): \n",
" x = random.randrange(1,8)\n",
" print(x)"
],
"metadata": {
"id": "MDcOH5WRzJLG"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Inspect the results you got from running the codes above. What do you conclude about what the random.randrange() function does? If we want to generate numbers in the range 0-100 inclusive, what should be the start and stop values of the range? Write your answers in the cell below."
],
"metadata": {
"id": "V429Jl_CzOEd"
}
},
{
"cell_type": "markdown",
"source": [
"#### Exercise 2\n",
"\n",
"Now you are ready to write a Hi-Lo game. Your game should: \n",
"\n",
" 1. Choose a secret number in the range 0-100 inclusive.\n",
" 1. Ask the user for a guess. \n",
" 1. Keep allowing the user to make guesses until the user guesses the correct number. \n",
"\n",
"Hint: any variable you check in your while loop's boolean condition needs to already be initialized with a value. That means that you'll have to ask for the first guess outside your main loop and then ask for subsequent guesses inside the body of the loop. "
],
"metadata": {
"id": "E6WI7SEdzTo0"
}
},
{
"cell_type": "code",
"source": [
"# write your code here"
],
"metadata": {
"id": "t6h-YgFtzkBR"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#### Exercise 3\n",
"\n",
"Test your game and be sure it is working. You want to be sure that you've tested every line of code. This is harder than it seems, since your game could pick any random number between 0-100. Spend a minute discussing techniques you could use to make it easier to test your code for correctness. \n",
"\n",
"**Hints:** Temporarily change the range to something smaller so that you can effectively test to see that your program detects a correct answer. \n",
"\n",
"Temporarily have the game print out the random number it has chosen so that you know what guess should give you the right answer. \n",
"\n",
"Since computers cannot really generate random numbers, it is done by \"seeding\" the random number generator with a value that will help it produce a different output each time it is asked for a number. By default, Python uses the current time and date to give a unique seed each time. If you seed the random number generator with a fixed seed, it will always generate the same sequence of numbers. This could be helpful for testing. To seed the generator, use the following where x is an integer you choose: \n",
"\n",
"`random.seed(x)`\n"
],
"metadata": {
"id": "4ztLYnsAzlGo"
}
},
{
"cell_type": "code",
"source": [
"# write your code here"
],
"metadata": {
"id": "OPWBjrmEz-g7"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment