import random
my_win = 0
my_loss = 0
my_tie = 0
def game():
global my_tie, my_loss, my_win
var = {'scissors':(0,1,0),
'paper':(0,0,1),
'rock':(1,0,0)}
user = input("Please choice scissors, paper or rock: ")
while user not in ['scissors', 'paper', 'rock']:
user = input("Please choice scissors, paper or rock: ")
computer = random.choice(['scissors','paper','rock'])
print(f"USER - {user} \nCOMPUTER - {computer}")
for k, v in var.items():
if k == user:
one = int(v.index(1))
if k == computer:
two = int(v.index(1))
if one < two:
print(f"USER with {user} - WIN!")
my_win += 1
elif one == two:
print("==TIE==")
my_tie += 1
else:
print(f"COMPUTER with {computer} - WIN!")
my_loss += 1
def results():
print ("You win %d times!" % my_win)
print ("You lose %d times!" % my_loss)
print ("You tie %d times!" % my_tie)
if __name__ == "__main__":
game()
UPDATE
After code review I'm rewrite my code to this. Added multiple starting code and right printing results. Thank for all comments and help. This is great experience for me!
import random
num_wins = 0
num_losses = 0
num_ties = 0
def run_game():
global num_wins, num_losses, num_ties
choices = ["paper", "scissors", "rock"]
user_move = input("Please choice scissors, paper or rock: ")
while user_move not in choices:
user_move = input("Please choice scissors, paper or rock: ")
comp_move = random.choice(choices)
if user_move == comp_move:
num_ties += 1
print(f"BOTH with {user_move} - TIE")
elif choices[(choices.index(user_move) + 1) % len(choices)] == comp_move:
num_losses += 1
print(f"COMPUTER with {comp_move} - WIN!")
else:
num_wins += 1
print(f"USER with {user_move} - WIN!")
def print_results():
print("You win %d times!" % num_wins)
print("You lose %d times!" % num_losses)
print("You tie %d times!" % num_ties)
if __name__ == "__main__":
end = ""
while end.lower() != "y":
run_game()
end = input("Enter 'y'/'Y' to continue ")
print_results()