'I am trying to make 2 person game
Hello I am tryna make 2 person guess the number game but I dont know how to show the win message can someone help?
guessp1 = int(input(player1 + "'s turn = "))
if guessp1 > answer:
print(player1,"has to guess lower!")
time.sleep(2)
else:
print(player1,"has to guess higher!")
time.sleep(2)
guessp2 = int(input(player2 + "'s turn = "))
if guessp2 > answer:
print(player2,"has to guess lower!")
time.sleep(2)
else:
print(player2,"has to guess higher!")
time.sleep(2)
guessp1 = int(input(player1 + "'s turn = "))
while guessp1 & guessp2 != answer:
if guessp1 > answer:
print(player1,"has to guess lower!")
time.sleep(2)
else:
print(player1,"has to guess higher!")
time.sleep(2)
guessp2 = int(input(player2 + "'s turn = "))
if guessp2 > answer:
print(player2,"has to guess lower!")
time.sleep(2)
else:
print(player2,"has to guess higher!")
time.sleep(2)
guessp1 = int(input(player1 + "'s turn = "))
It always makes the first player winner.
Solution 1:[1]
If you want to keep what you have because you wrote it and understand it, just change all your if's and else's for guessp1 and guessp2 to this:
if guessp1 == answer:
print("You win")
quit()
elif guessp1 > answer:
print(player1,"has to guess lower!")
time.sleep(2)
else:
print(player1,"has to guess higher!")
time.sleep(2)
You might also change your while loop to 'guessp1 or guessp2' instead of 'guessp1 and guessp2'. Good luck!
Solution 2:[2]
This all can be done simpler, if you would learn how to write your own functions, to get rid of repetitive code. Here I make a function, that takes a name of the current player and the answer, inside the function I ask for input and check the answer. If it's correct, True
is returned from the function, otherwise False
. Then outside the function you check if the winner is found, and on every step swap players
import time
import random
def make_turn_and_check(current_player, answer):
time.sleep(2)
guess = int(input(current_player + "'s turn = "))
if guess > answer:
print(current_player, "has to guess lower!")
elif guess < answer:
print(current_player, "has to guess higher!")
else:
return True # winner!
return False # not winner
answer = random.randint(1,100)
current_player, next_player = 'Bob', 'Alice'
while True:
# make next player current, and current player next
current_player, next_player = next_player, current_player
if make_turn_and_check(current_player, answer):
break # winner found
print('The winner is', current_player)
Comments:
while True:
means an infinite loop, it won't stop looping, until you execute break
.
Syntax a, b = b, a
means to simultaneously assign a = b
and b = a
, but if it would be 2 different lines, for example:
a = 'Alice'
b = 'Bob'
a = b
b = a
then the result would be wrong, because a
was already assigned to Bob
on the first line, so after the second line, both a
and b
would be Bob
. Using syntax a, b = b, a
the values would be swapped, b
would be Alice
and a
would be Bob
.
In my code I used it to make the current player out of the next player, and assign the current player to be the next player.
Finally line
if make_turn_and_check(current_player, answer):
break
makes a call to the function above, and since the function returns True
if the winner is found, then in the if
statement we get to execute line break
, finishing the game. Otherwise we skip the break
and go to the next iteration of the loop.
To better understand, you can assign the resulting True
/False
value from the function to an intermediate variable:
is_current_winner = make_turn_and_check(current_player, answer)
if is_current_winner:
break
is_current_winner
will be a boolean variable containing True
or False
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |