-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjumble-word.py
162 lines (116 loc) · 3.37 KB
/
jumble-word.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Python program for jumbled words game.
# import random module
import random
# function for choosing random word.
def choose():
# list of word
words = ['rainbow', 'computer', 'science', 'programming',
'mathematics', 'player', 'condition', 'reverse',
'water', 'board', 'geeks']
# choice() method randomly choose
# any word from the list.
pick = random.choice(words)
return pick
# Function for shuffling the
# characters of the chosen word.
def jumble(word):
# sample() method shuffling the characters of the word
random_word = random.sample(word, len(word))
# join() method join the elements
# of the iterator(e.g. list) with particular character .
jumbled = ''.join(random_word)
return jumbled
# Function for showing final score.
def thank(p1n, p2n, p1, p2):
print(p1n, 'Your score is :', p1)
print(p2n, 'Your score is :', p2)
# check_win() function calling
check_win(p1n, p2n, p1, p2)
print('Thanks for playing...')
# Function for declaring winner
def check_win(player1, player2, p1score, p2score):
if p1score > p2score:
print("winner is :", player1)
elif p2score > p1score:
print("winner is :", player2)
else:
print("Draw..Well Played guys..")
# Function for playing the game.
def play():
# enter player1 and player2 name
p1name = input("player 1, Please enter your name :")
p2name = input("Player 2 , Please enter your name: ")
# variable for counting score.
pp1 = 0
pp2 = 0
# variable for counting turn
turn = 0
# keep looping
while True:
# choose() function calling
picked_word = choose()
# jumble() function calling
qn = jumble(picked_word)
print("jumbled word is :", qn)
# checking turn is odd or even
if turn % 2 == 0:
# if turn no. is even
# player1 turn
print(p1name, 'Your Turn.')
ans = input("what is in your mind? ")
# checking ans is equal to picked_word or not
if ans == picked_word:
# incremented by 1
pp1 += 1
print('Your score is :', pp1)
turn += 1
else:
print("Better luck next time ..")
# player 2 turn
print(p2name, 'Your turn.')
ans = input('what is in your mind? ')
if ans == picked_word:
pp2 += 1
print("Your Score is :", pp2)
else:
print("Better luck next time...correct word is :", picked_word)
c = int(input("press 1 to continue and 0 to quit :"))
# checking the c is equal to 0 or not
# if c is equal to 0 then break out
# of the while loop o/w keep looping.
if c == 0:
# thank() function calling
thank(p1name, p2name, pp1, pp2)
break
else:
# if turn no. is odd
# player2 turn
print(p2name, 'Your turn.')
ans = input('what is in your mind? ')
if ans == picked_word:
pp2 += 1
print("Your Score is :", pp2)
turn += 1
else:
print("Better luck next time.. :")
print(p1name, 'Your turn.')
ans = input('what is in your mind? ')
if ans == picked_word:
pp1 += 1
print("Your Score is :", pp1)
else:
print("Better luck next time...correct word is :", picked_word)
c = int(input("press 1 to continue and 0 to quit :"))
if c == 0:
# thank() function calling
thank(p1name, p2name, pp1, pp2)
break
c = int(input("press 1 to continue and 0 to quit :"))
if c == 0:
# thank() function calling
thank(p1name, p2name, pp1, pp2)
break
# Driver code
if __name__ == '__main__':
# play() function calling
play()