I would suggest putting the "get a number from user" into a function and add some exception handling for dumb players:
import random
def get_numeric_input(text,num_range):
i = -1
while i < 0:
k = input(text)
try:
i = int(k)
if i in num_range:
return i
else:
i = -1
raise ValueError
except ValueError:
print("Only numbers in range: ",list(num_range))
You can learn more about this in the answers to Asking the user for input until they give a valid response.
You should only ask the user once for his name, put the rest of your games logic into a while
with a suitable codition to leave it - I choose 5 times lost is enough.
Collect wins/draws/looses into variables. I added a dictionary for numer to choice mapping and some string.format() for nicer output - see:
User1 = input('What s your name?>>>')
print("Lets start a single player mode of rock, paper, scissors, lizard and Spock.")
lost = 0
win = 0
tie= 0
# dictionary lookup for nicer outputs - maps number to text
d = { 0:"rock", 1:"paper", 2:"scissors", 3:"lizard", 4:"Spock"}
# define some texts once if they repeat all the time
rules = ('''
Choose one of the options below {} - use number only:
0. rock
1. paper
2. scissors
3. lizard
4. Spock
Your choice? ''')
userwins = User1 + " wins!"
while lost < 5:
# fully automatic play - use the commented instead for human players
player_num = random.randrange(0, 5) # get_numeric_input(rules,range(5))
computer_num = random.randrange(0, 5)
# always print what was choosen
print("You took {} - the computer choose {}.".format(d[player_num], d[computer_num]),
end=" " )
difference = (player_num - computer_num) % 5
if difference == 0:
tie += 1
print('It´s a tie')
elif difference in (1,2):
win += 1
print(userwins)
else:
lost += 1
print('Computer wins.')
print("You won {} and lost {} times. You tied {} times. Overall you are the {}.".format(
win, lost, tie, "Winner" if win > lost else "Looser" ))
For the actual game logic - your mapping seems to be incorrect. The rules of the game say:
def did_I_win(me,pc):
# 0 rock = scissor, lizard
# 1 paper = rock, spock
# 2 scissor = lizard, paper
# 3 lizard = paper, spock
# 4 spock = rock, scissor
wins = {0:{2,3}, 1:{0,4},2:{1,3},3:{1,4},4:{0,2}}
return pc in wins[me]
So if you change your main program to:
while lost < 5:
# fully automatic play - use the commented instead for human players
player_num = random.randrange(0, 5) # get_numeric_input(rules,range(5))
computer_num = random.randrange(0, 5)
# always print what was choosen
print("You took {} - the computer choose {}.".format(d[player_num], d[computer_num]),
end=" " )
difference = (player_num - computer_num) % 5
if player_num == computer_num:
tie += 1
print('It´s a tie')
elif did_I_win(player_num,computer_num):
win += 1
print(userwins)
else:
lost += 1
print('Computer wins.')
print("You won {} and lost {} times. You tied {} times. Overall you are the {}.".format(
win, lost, tie, "Winner" if win > lost else "Looser" ))
it works as expected:
What s your name?>>>Joe
Lets start a single player mode of rock, paper, scissors, lizard and Spock.
You took Spock - the computer choose rock. Joe wins!
You took rock - the computer choose Spock. Computer wins.
You took paper - the computer choose scissors. Computer wins.
You took paper - the computer choose paper. It´s a tie
You took paper - the computer choose lizard. Computer wins.
You took rock - the computer choose Spock. Computer wins.
You took rock - the computer choose Spock. Computer wins.
You won 1 and lost 5 times. You tied 1 times. Overall you are the Looser.
Your current logic gives you:
Result ok?
rock vs paper = (0-1) % 5 = 4 lost ok
rock vs scissors = (0-2) % 5 = 3 win ok
rock vs lizard = (0-3) % 5 = 2 win ok
rock vs Spock = (0-4) % 5 = 1 lost ok
paper vs rock = (1-0) % 5 = 1 win ok
paper vs scissors = (1-2) % 5 = 4 lost ok
paper vs lizard = (1-3) % 5 = 3 lost no: earlier 3 was win
paper vs Spock = (1-4) % 5 = 2 win ok
scissors vs rock = (2-0) % 5 = 2 lost no: earlier 2 was win
scissors vs paper = (2-1) % 5 = 1 ... etc ...
scissors vs lizard = (2-3) % 5 = 4
scissors vs Spock = (2-4) % 5 = 3
lizard vs rock = (3-0) % 5 = 3
lizard vs paper = (3-1) % 5 = 2
lizard vs scissors = (3-2) % 5 = 1
lizard vs Spock = (3-4) % 5 = 4
Spock vs rock = (4-0) % 5 = 4
Spock vs paper = (4-1) % 5 = 3
Spock vs scissors = (4-2) % 5 = 2
Program:
d= { 0:"rock",1:"paper",2:"scissors",3:"lizard",4:"Spock"}
# rock = scissor, lizard
# paper = rock, spock
# scissor = lizard, paper
# lizard= paper, spock
# spock = rock, scissor
print("------")
for i in range(5):
for j in range(5):
if i!=j:
print("{:<10} vs {:<10} = ({}-{}) % 5 = {}".format(
d[i],d[j],i,j,(i-j)%5))