-
Notifications
You must be signed in to change notification settings - Fork 1
/
pythonGame.txt
58 lines (51 loc) · 1.7 KB
/
pythonGame.txt
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
import random
name = input("Enter your name = ")
print(f"Hello {name}! Welcome to the guessing game!")
words = ["guessing","apple","television","earphones","apple","macbook","python","sunset","opensource","hp"]
index = random.randint(0, len(words))
word = words[index]
indexes = random.sample(range(0, len(words)), 3)
guesses = ""
for i in indexes:
guesses += word[i]
chances = 10
play = "Yes"
def playagain():
#this will help ypu understand scoping
global play
play = input("Do you want to play again? (Yes/No): ")
if play == "Yes":
global chances, word, guesses
chances = 10
index = random.randint(0, len(words))
word = words[index]
indexes = random.sample(range(0, len(words)), 3)
guesses = ""
for i in indexes:
guesses += word[i]
#This play is global variable
while play == "Yes":
while chances > 0:
won = True
for ch in word:
if ch in guesses: #the person has guessed
print(ch, end=" ")
else:
print("_", end=" ")
won = False
if won:
print("\nYou won!")
print(f"Your score is {chances * 10})")
playagain() #ask we want to play again
break
# take a guess from the user
guess = input("\nGuess a character: ")
guesses += guess
if guess not in word:
chances -= 1
print("\nWrong Answer!!")
print(f"\nYou have {chances} chances left!")
if chances == 0:
print("You lose!!")
playagain() #ask we want to play again
break