-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguessing_game.py
More file actions
106 lines (80 loc) · 3.29 KB
/
guessing_game.py
File metadata and controls
106 lines (80 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
Python Web Development Techdegree
Project 1 - Number Guessing Game
--------------------------------
For this first project we will be using Workspaces.
NOTE: If you strongly prefer to work locally on your own computer, you can totally do that by clicking: File -> Download Workspace in the file menu after you fork the snapshot of this workspace.
"""
import random
# set high score
high_score = 10
def start_game():
"""Psuedo-code Hints
When the program starts, we want to:
------------------------------------
1. Display an intro/welcome message to the player.
2. Store a random number as the answer/solution.
3. Continuously prompt the player for a guess.
a. If the guess greater than the solution, display to the player "It's lower".
b. If the guess is less than the solution, display to the player "It's higher".
4. Once the guess is correct, stop looping, inform the user they "Got it"
and show how many attempts it took them to get the correct number.
5. Let the player know the game is ending, or something that indicates the game is over.
( You can add more features/enhancements if you'd like to. )
"""
# write your code inside this function.
# Kick off the program by calling the start_game function.
# creating a function that asks for guesses while adding to trys
def prompter():
nonlocal trys
trys += 1
return input("Choose a number from 1 - 1000\n")
# declairing game var
trys = 0
answer = random.randint(1, 1000)
# declairing guess so fails first loop
guess = 0
# while loop that ends when player answers correct number
while guess != answer:
# prompting palyer for a guess
guess = prompter()
# while loop making sure that the number entered is a valid number
while type(guess) != int:
try:
guess = int(guess)
except ValueError:
print("Please enter a valid number")
guess = prompter()
# letting user know if they guessed higher or lowwer than answer and checking if guess is within the range(1 - 10)
if guess < 1 or guess > 1000:
print("number was not between 1 and 1000")
guess = prompter()
elif guess < answer:
print("Higher")
elif guess > answer:
print("Lowwer")
# after loop let player know how many tries it took them to get the correct answer and if they got the high score
print(f"you got it in {trys} try(s)")
global high_score
if high_score > trys:
high_score = trys
print(f"Wow!!! you just made the high score")
elif high_score < trys:
print(f"so close the high score was {high_score}, you were just {trys - high_score} away from breaking the record")
else:
print("you tied the high score")
print("Glad you joined us. We're gonna be playing a guissing game")
start_game()
# function that answers if player would like to play again
def replay():
replay = input("would you like to play again?\n(yes/no) ")
if replay == "yes":
return True
else:
return False
again = replay()
# loop enabling multiple games
while again:
start_game()
again = replay()
print("Hate to see you leave, Goodbye!")