-
Notifications
You must be signed in to change notification settings - Fork 1
/
practiceProblem-6.py
125 lines (103 loc) · 3.47 KB
/
practiceProblem-6.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
# “Guess The number”
# Problem Statement:-
# Generate a random integer from a to b. You and your friend have to guess a number between two numbers a and b. a and b are inputs taken from the user. Your friend is player 1 and plays first. He will have to keep choosing the number and your program must tell whether the number is greater than the actual number or less than the actual number. Log the number of trials it took your friend to arrive at the number. You play the same game and then the person with minimum number of trials wins! Randomly generate a number after taking a and b as input and don’t show that to the user.
#
# Input:
# Enter the value of a
#
# 4
#
# Enter the value of b
#
# 13
#
# Output:
# Player1 :
#
# Please guess the number between 4 and 13
#
# 5
#
# Wrong guess a greater number again
#
# 8
#
# Wrong guess a smaller number again
#
# 6
#
# Correct you took 3 trials to guess the number
#
# Player 2:
#
# Correct you took 7 trials to guess the number
#
# Player 1 wins!
#------------------------- ----------------------------
import random
def rand_generator():
return random.randint(a,b)
if __name__ == '__main__':
a =int(input(' Enter the Lowerlimit:\n '))
b = int(input(' Enter the Upperlimit:\n '))
player1 = input('Enter the name of player1 :\n')
player2 = input('Enter the name of player2 :\n')
player = player1
answer =rand_generator()
print(f'{player}\'s turn-\n')
counter =0
while True:
print(f'please guess number between {a} and {b}')
guess = int(input('Enter your Guess:\n'))
counter += 1
if guess == answer:
print('Correct')
print(f'you took {counter} turns to guess')
if player == player2:
player2_counter = counter
break
player = player2
player1_counter = counter
print(f'{player}\'s turn-\n')
counter =0
answer =rand_generator()
elif guess<answer:
print(f'Try entering a greater number')
elif guess>answer:
print(f'Try entering a smaller number')
if player1_counter>player2_counter:
print(f'\n{player2} has won !!')
elif player1_counter<player2_counter:
print(f'\n{player1} has won !!')
elif player1_counter == player2_counter:
print('\nMatch draw. Both are Winners')
#CWH's code -
# import random
def guessGame(a, b, actual):
guess = int(input(f"Guess a number between {a} and {b}\n"))
nguess = 1
while guess != actual:
if guess< actual:
guess = int(input(f"Enter a bigger number\n"))
nguess += 1
else:
guess = int(input(f"Enter a smaller number\n"))
nguess += 1
print(f"You guessed the number in {nguess} guesses\n")
return nguess
if __name__ == "__main__":
a = int(input("Enter the value of a\n"))
b = int(input("Enter the value of b\n"))
actual1 = random.randint(a, b)
print("Player 1's turn\n")
g1 = guessGame(a, b, actual1)
print("Player 2's turn\n")
actual2 = random.randint(a, b)
g2 = guessGame(a, b, actual2)
if g1 < g2:
print("Player 1 won the match!\n")
elif g1 > g2:
print("Player 2 won the match!\n")
else:
print("Its a Tie!\n")
print(f"The number for player 1 was {actual1} and for player 2 was {actual2}")