guessing game python

Solutions on MaxInterview for guessing game python by the best coders in the world

showing results for - "guessing game python"
Michela
18 Jun 2016
1# updated version
2import random
3# for instructions so that the user understands
4def instructions():
5    print("Welcome to the guessing game you will have 3 tries to pick a number 1-10")
6    print("Good luck btw it's all random")
7
8
9instructions()
10# guess limit so the user can't guess too much.
11guess_limit = 1
12# The random guess
13number = random.randint(1, 10)
14# What users can type and see.
15user = int(input("What is the number?: "))
16# The while loop so it can go on.
17while user != number:
18
19    if user > number:
20        print("Lower")
21
22    elif user < number:
23        print("Higher")
24
25    user = int(input("What is the number?: "))
26    guess_limit += 1
27    if guess_limit == 3:
28        print("------------------------------------------------------")
29        print("You ran out of guess ;( the answer was", number, "<--")
30        print("------------------------------------------------------")
31        break
32else:
33    print("You guessed it right! The number is", number,
34          "and it only took you ", guess_limit, "tries")