{ "cells": [ { "cell_type": "markdown", "id": "a21a577a", "metadata": {}, "source": [ "# Hello" ] }, { "cell_type": "code", "execution_count": null, "id": "cf5ba095", "metadata": {}, "outputs": [], "source": [ "\"\"\"\n", " Simple guess game that will loop until you get it right.\n", " Be very careful that you follow the indentation here.\n", "\"\"\"\n", "\n", "\n", "import random\n", "\n", "def main():\n", " correct = random.randint(1,9) # the randon correct value\n", " print(\"Guess the number between 1 and 9\")\n", "\n", " number_of_tries = 0\n", "\n", " while True: # Start of infinite loop\n", " guess = int(input(\"Have a guess (give an int) : \"))\n", " number_of_tries += 1\n", "\n", " if guess == correct: # Test if the guess is correct\n", " print(\"You guessed right\")\n", " break # If correct break the loop\n", " else:\n", " print(\"You guessed wrong.... have another go\")\n", " # End of loop\n", "\n", "\n", " # This is after loop.\n", " print(\"You took \" + str(number_of_tries) + \" attempt to guess right\")\n", "\n", "\n", "main()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }