Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 196 additions & 0 deletions code-explained.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Guess the number!\n",
"\n",
"This is a _Guess the number_ game. To play, you will have to choose between 3 levels of difficulty, or choose the rules yourself! The computer will generate a random number and you will have to guess it. \n",
"The code will tell you whether your guess is below or above the correct number, but be careful, you only have a limited number of tries!\n",
"\n",
"__Good luck!__"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Def ask_new_round() and set it to \"yes\" for first round:\n",
"def ask_new_round():\n",
" choice=input(\"Do you want to play another round? \").lower()\n",
" while choice not in [\"yes\", \"no\"]:\n",
" choice=input(\"Please, say yes or no: \").lower()\n",
" return choice\n",
"new_round=\"yes\"\n",
"\n",
"#Start game\n",
"while new_round==\"yes\":\n",
" \n",
" #Ask for the level they want to play\n",
" levels=[\"easy\", \"medium\", \"hard\", \"let me choose\"]\n",
" def ask_level():\n",
" choice=input(\"\\nPick your level: easy, medium or hard? If you want to choose the rules yourself, please type 'let me choose' \").lower()\n",
" while choice not in levels:\n",
" print(\"\\nPlease, choose one of the options\")\n",
" choice=input(\"\\nPick your level: easy, medium or hard? If you want to choose the rules yourself, please type 'let me choose' \").lower()\n",
" return choice\n",
" level=ask_level()\n",
"\n",
" #Set range of numbers and number of tries according to the level chosen\n",
" if level==\"easy\":\n",
" numbers=list(range(1,11))\n",
" tries=4\n",
" print(\"Great! You'll have to pick a number between 1 and 10. You have 4 turns to get it right. Let's play!\\n\")\n",
" elif level==\"medium\":\n",
" numbers=list(range(1,101))\n",
" tries=7\n",
" print(\"Great! You'll have to pick a number between 1 and 100. You have 7 turns to get it right. Let's play!\\n\")\n",
" elif level==\"hard\":\n",
" numbers=list(range(1,1001))\n",
" tries=10\n",
" print(\"Great! You'll have to pick a number between 1 and 1000. You have 10 turns to get it right. Let's play!\\n\")\n",
" else:\n",
" #Define variables for \"let me choose\" option\n",
" print(\"\\nGreat! You'll have to choose the minimum and maximum numbers you want the game to have, and how many turns do you want to have to guess it. Let's play!\\n\")\n",
" \n",
" range_num=range(-100000,100001)\n",
" range_str=[]\n",
" for i in range_num:\n",
" range_str.append(str(i))\n",
"\n",
" range_tries_good=list(range(1,21))\n",
" range_tries_good_str=[]\n",
" for i in range_tries_good:\n",
" range_tries_good_str.append(str(i))\n",
"\n",
" range_tries_bad=list(range(21,101))\n",
" range_tries_bad_str=[]\n",
" for i in range_tries_bad:\n",
" range_tries_bad_str.append(str(i))\n",
" \n",
" total_tries_range=range_tries_good_str+range_tries_bad_str\n",
" \n",
" #Ask for minimum number in the range, function will only take numbers\n",
" def custom_range_min():\n",
" choice=input(\"\\nChoose the minimum value of your range: \")\n",
" while choice not in range_str:\n",
" choice=input(\"Please, choose a number: \")\n",
" return int(choice)\n",
"\n",
" #Ask for minimum number in the range, function will only take numbers higher than the minimum set up before\n",
" def custom_range_max():\n",
" choice=input(\"\\nChoose the maximum value of your range: \")\n",
" while choice not in range_str:\n",
" choice=input(\"Please, choose a number higher than {}: \".format(range_min))\n",
" while choice in range_str and int(choice)<=range_min:\n",
" if choice in range_str:\n",
" choice=input(\"Please, choose a number higher than {}: \".format(range_min))\n",
" while choice not in range_str:\n",
" choice=input(\"Please, choose a number higher than {}: \".format(range_min))\n",
" else:\n",
" return int(choice)\n",
"\n",
" #Ask for number of tries. Maximum available is 100. If tries>20, the code will display a warning message. \n",
" def custom_tries():\n",
" choice=input(\"\\nNow choose how many tries you want to have: \")\n",
" while choice not in total_tries_range:\n",
" choice=input(\"\\nPlease, choose a number between 1 and 100: \")\n",
" if choice in range_tries_bad_str:\n",
" answer=input(\"Wow, this a very high number of tries, are you sure you want to proceed? \").lower()\n",
" if answer==\"yes\":\n",
" return int(choice)\n",
" elif answer==\"no\":\n",
" choice=input(\"\\nOkay then, choose again a number between 1 and 100: \")\n",
" return int(choice)\n",
" while answer not in [\"yes\", \"no\"]:\n",
" answer=input(\"Please, say yes or no \")\n",
" if choice in range_tries_good_str:\n",
" return int(choice)\n",
" if choice in range_tries_bad_str:\n",
" answer=input(\"Wow, this a very high number of tries, are you sure you want to proceed? \").lower()\n",
" if answer==\"yes\":\n",
" return int(choice)\n",
" elif answer==\"no\":\n",
" choice=input(\"\\nPlease, choose a number between 1 and 100: \")\n",
" return int(choice)\n",
" while answer not in [\"yes\", \"no\"]:\n",
" answer=input(\"Please, say yes or no \")\n",
" elif choice in range_tries_good_str:\n",
" return int(choice)\n",
"\n",
" #Assign inputs from user to variables numbers and tries\n",
" custom_range_min\n",
" range_min=custom_range_min()\n",
" range_max=custom_range_max()\n",
" numbers=list(range(range_min,range_max+1))\n",
" tries=custom_tries()\n",
" \n",
" #Generate random correct number\n",
" import random\n",
" correct_number=random.choice(numbers)\n",
" \n",
" #Define function for user's guessed number\n",
"\n",
" #Making a list of the number range chosen, each number stored as a string so that then it works when using input(), as the answer from the user will be stored as a string. We need this to make sure the input they put is in the range of numbers chosen.\n",
" str_numbers=[]\n",
" for i in numbers:\n",
" str_numbers.append(str(i))\n",
" \n",
" #Ask user for number and then turn it into a integer to compare it with the correct value\n",
" def ask_number():\n",
" choice1=input(\"Pick a number between {} and {}: \".format(str(min(numbers)), str(max(numbers))))\n",
" while choice1 not in str_numbers:\n",
" choice1=input(\"Please, pick a number between {} and {}: \".format(str(min(numbers)), str(max(numbers))))\n",
" return int(choice1)\n",
"\n",
" #Run game\n",
" tries_count=0\n",
" for i in range(0, tries):\n",
" guess_number=ask_number()\n",
" if guess_number==correct_number:\n",
" print(\"Congratulations! You guessed the number!\\n\")\n",
" break\n",
" elif guess_number<correct_number and tries_count<tries-1:\n",
" tries_count+=1\n",
" print(\"You are too low, try again! Tries left:\", tries-tries_count, \"\\n\")\n",
" continue\n",
" elif guess_number>correct_number and tries_count<tries-1:\n",
" tries_count+=1\n",
" print(\"You are too high, try again! Tries left:\", tries-tries_count, \"\\n\")\n",
" continue\n",
" else:\n",
" print(\"You don't have any tries left, you lose the game. The correct number was\", correct_number, \"\\n\")\n",
" \n",
" #Ask if the want to play another round\n",
" new_round=ask_new_round()\n",
"\n",
"else:\n",
" print(\"Okay, thanks for playing the game. See you next time!\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading