Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

guessing game python

import random

num = random.randint(1, 9)

while True:

    try:
        guess = int(input("
Please enter a guess from 1-9: "))

        if 0 < guess < 10:

            if guess > num:
                print("
You guessed too high")

            elif guess < num:
                print("
You guessed too low")

            elif guess == num:
                print("
You guessed correctly")

                while True:
                    u_input = input("
Would you like to play again? y/n: ")

                    if u_input == 'n':
                        exit()

                    elif u_input == 'y':
                        num = random.randint(1, 9)
                        break

                    elif u_input != 'y' and u_input != 'n':
                        print("
Error: Please select a valid option")

        elif guess < 1 or guess > 9:
            print("
Error: Please enter a number from 1-9")

    except ValueError:
        print("
Error: Please enter a number")
Source by gist.github.com #
 
PREVIOUS NEXT
Tagged: #guessing #game #python
ADD COMMENT
Topic
Name
3+3 =