Rock paper scissors game how to make it infinite
Question:
How can I make it so the game is infinite? and is there a way to simplify this code?
I have tried to work around but can’t seem to figure it out.
# A rock paper scissors game.
import random
Move1=input("Enter your move: (r)ock (p)aper (s)cissors or (q)uit: ").lower()
Move2=["r","p","s"]
while Move1 != "q":
if Move1 == "r" or "p" or "s" or "q":
# print(random.choice(Move2))
Move2=random.choice(Move2)
if Move1=="r" and Move2=="s":
print("You've won")
break
elif Move2=="p":
print("You lost!")
break
elif Move2=="r":
print("You went even!")
break
if Move1=="p" and Move2=="s":
print("You lost!")
break
elif Move2=="p":
print("You went even!")
break
elif Move2=="r":
print("You won!")
break
if Move1=="s" and Move2=="s":
print("You went even!")
break
elif Move2=="p":
print("You won!")
break
elif Move2=="r":
print("You lost!")
break
else:
print("You've quit the game!")
exit()
Tried to remove break
Answers:
You have to re-evaluate the input at the end of the while loop. Or you put it into the while condition. So you could do while (Move1 := input(...)) != "q"
.
Also your first if check is always true because of the or "p"
. You would have to do or Move1 == "p" or Move1 == "s" or Move1 == "q"
You could simplify it with if Move1 in {"r", "p", "s", "q"}
Move the request for input inside the loop. Use while True:
around the loop to make it infinite. Remove all the break
statements after reporting the winners and losers.
Don’t use the same variable Move2
for the list of moves and the computer’s move. That will prevent making a computer choice on the 2nd round.
# A rock paper scissors game.
import random
allowed_moves=["r","p","s"]
while True:
player_move=input("Enter your move: (r)ock (p)aper (s)cissors or (q)uit: ").lower()
if player_move in allowed_moves:
Move2=random.choice(allowed_moves)
if player_move=="r" and Move2=="s":
print("You've won")
elif Move2=="p":
print("You lost!")
elif Move2=="r":
print("You went even!")
if player_move=="p" and Move2=="s":
print("You lost!")
elif Move2=="p":
print("You went even!")
elif Move2=="r":
print("You won!")
if player_move=="s" and Move2=="s":
print("You went even!")
elif Move2=="p":
print("You won!")
elif Move2=="r":
print("You lost!")
else if player_move == 'q':
print("You've quit the game!")
break
else:
print("That's not a valid move!")
Abstracting some of the game logic to a function is a start in the direction of simplifying. But there are other ways to simplify. Here’s an example of it along with what I think you mean by making the game infinite.
def play(move1, move2):
# the logic
print("you win")
# more logic
is_playing = True
while is_playing:
Move1 = input(
"Enter your move: (r)ock (p)aper (s)cissors or (q)uit: ").lower()
Move2 = ["r", "p", "s"]
if Move1 == 'q':
print("You've quit the game!")
is_playing = False
break
play(Move1, Move2)
Let me know if that helps or not
How can I make it so the game is infinite? and is there a way to simplify this code?
I have tried to work around but can’t seem to figure it out.
# A rock paper scissors game.
import random
Move1=input("Enter your move: (r)ock (p)aper (s)cissors or (q)uit: ").lower()
Move2=["r","p","s"]
while Move1 != "q":
if Move1 == "r" or "p" or "s" or "q":
# print(random.choice(Move2))
Move2=random.choice(Move2)
if Move1=="r" and Move2=="s":
print("You've won")
break
elif Move2=="p":
print("You lost!")
break
elif Move2=="r":
print("You went even!")
break
if Move1=="p" and Move2=="s":
print("You lost!")
break
elif Move2=="p":
print("You went even!")
break
elif Move2=="r":
print("You won!")
break
if Move1=="s" and Move2=="s":
print("You went even!")
break
elif Move2=="p":
print("You won!")
break
elif Move2=="r":
print("You lost!")
break
else:
print("You've quit the game!")
exit()
Tried to remove break
You have to re-evaluate the input at the end of the while loop. Or you put it into the while condition. So you could do while (Move1 := input(...)) != "q"
.
Also your first if check is always true because of the or "p"
. You would have to do or Move1 == "p" or Move1 == "s" or Move1 == "q"
You could simplify it with if Move1 in {"r", "p", "s", "q"}
Move the request for input inside the loop. Use while True:
around the loop to make it infinite. Remove all the break
statements after reporting the winners and losers.
Don’t use the same variable Move2
for the list of moves and the computer’s move. That will prevent making a computer choice on the 2nd round.
# A rock paper scissors game.
import random
allowed_moves=["r","p","s"]
while True:
player_move=input("Enter your move: (r)ock (p)aper (s)cissors or (q)uit: ").lower()
if player_move in allowed_moves:
Move2=random.choice(allowed_moves)
if player_move=="r" and Move2=="s":
print("You've won")
elif Move2=="p":
print("You lost!")
elif Move2=="r":
print("You went even!")
if player_move=="p" and Move2=="s":
print("You lost!")
elif Move2=="p":
print("You went even!")
elif Move2=="r":
print("You won!")
if player_move=="s" and Move2=="s":
print("You went even!")
elif Move2=="p":
print("You won!")
elif Move2=="r":
print("You lost!")
else if player_move == 'q':
print("You've quit the game!")
break
else:
print("That's not a valid move!")
Abstracting some of the game logic to a function is a start in the direction of simplifying. But there are other ways to simplify. Here’s an example of it along with what I think you mean by making the game infinite.
def play(move1, move2):
# the logic
print("you win")
# more logic
is_playing = True
while is_playing:
Move1 = input(
"Enter your move: (r)ock (p)aper (s)cissors or (q)uit: ").lower()
Move2 = ["r", "p", "s"]
if Move1 == 'q':
print("You've quit the game!")
is_playing = False
break
play(Move1, Move2)
Let me know if that helps or not