Issue
My code (rock, paper, scissors, lizard, Spock)is getting the right inputs but my function is only working sometimes, other times it gives me the output 'None'. Why is this happening?
I tried messing with the dictionaries and using different ways to get an output (like print and return) but it still sometimes gives me the output 'None' and I can't for the life of me figure out why.
import random
Num1 = random.randint(1,2)
def CPU():
return random.choice(["rock", "paper", "scissors", "lizard", "spock"])
def User_Choice():
User_Choice = input("What do you choose, rock, paper, scissors, lizard or spock?")
User_Choice = User_Choice.lower()
while User_Choice not in ["rock","paper","scissors","lizard","spock"]:
User_Choice = input("That is not an option, please try again")
User_Choice = User_Choice.lower()
return User_Choice
def Who_Wins(CPU_Choice,User_Choice1):
#put in CPU Choice first
if CPU_Choice == User_Choice1:
return("It was a draw, tension among the onlookers rises")
elif Num1 == 1:
Who_Wins1 = {
#The Zentharian Wins
("scissors","paper"):"You lose, The Zentharian chose scissors which cut up your paper",
("paper","rock"):"You lose, The Zentharian chose paper which covered your rock",
("rock","lizard"):"You lose, The Zentharian chose rock which crushed your lizard",
("lizard","spock"): "You lose, The Zentharian chose lizard which poisoned spock",
("spock","scissors"): "You lose, The Zentharian chose spock who smashed your scissors",
#Player Win
("scissors","lizard"): "You Win, your scissors decapitated the Zentharians's lizard",
("lizard","paper"): "You win, your lizard ate the Zentharians's paper",
("paper","spock"): "You win, your paper disproved the Zentharians's spock",
("spock","rock"): "You win, your spock vaporized the Zentharians's rock",
("rock","scissors"): "You win, your rock crushed the Zentharians's scissors"
}
if (CPU_Choice,User_Choice1) in Who_Wins1:
return Who_Wins1.get((CPU_Choice,User_Choice1),"no value returned")
elif Num1 == 2:
Who_Wins2 = {
#The AI Wins
("scissors","paper"):"You lose, The AI chose scissors which cut up your paper",
("paper","rock"):"You lose, The AI chose paper which covered your rock",
("rock","lizard"):"You lose, The AI chose rock which crushed your lizard",
("lizard","spock"): "You lose, The AI chose lizard which poisoned spock",
("spock","scissors"): "You lose, The AI chose spock who smashed your scissors",
#Player Win
("scissors","lizard"): "You Win, your scissors decapitated the AI's lizard",
("lizard","paper"): "You win, your lizard ate the AI's paper",
("paper","spock"): "You win, your paper disproved the AI's spock",
("spock","rock"): "You win, spock vaporized the AI's rock",
("rock","scissors"): "You win, your rock crushed the AI's scissors"
}
if (CPU_Choice,User_Choice1) in Who_Wins2:
return Who_Wins2.get((CPU_Choice,User_Choice1),"no value returned")
else:
return "at least you know what the error is now"
Who_Against()
CPU_Choice = CPU()
User_Choice1 = input("Choose rock, paper, scissors, lizard or spock ")
Winner = Who_Wins(CPU_Choice,User_Choice1)
print(Winner)
Solution
The output is None
when there isn't (CPU_Choice,User_Choice1)
in the dictionaries. When that happens, it doesn't enter the if
and there isn't any else
part for such case. As a result it reaches to the end of the function and returns None
.
For example, you have this tuple in your dictionaries: ("rock","lizard")
.
But what happens when it is the other way around and User_Choice1
is 'rock'
and CPU_Choice
is 'lizard'
?
If you removed the if
and have this
return Who_Wins2.get((CPU_Choice,User_Choice1),"no value returned")
or this
return Who_Wins1.get((CPU_Choice,User_Choice1),"no value returned")
returned without it, you would get the 'no value returned'
for such undefined cases
Edit:
Instead of the code duplication, you might want to use formatted string:
def Who_Wins(CPU_Choice,User_Choice1):
if CPU_Choice == User_Choice1:
return("It was a draw, tension among the onlookers rises")
else:
wins = {'cpu': ('lose', 'The %s chose', 'your'),
'user': ('win', 'your', "the %s's"),
}
machine = ['Zentharian', 'AI'][Num1 -1]
Who_Wins1 = {
("scissors","paper"):"scissors which cut up %s paper",
("paper","rock"):"paper which covered %s rock",
("rock","lizard"):"rock which crushed %s lizard",
("lizard","spock"): "lizard which poisoned %s spock",
("spock","scissors"): "spock who smashed %s scissors",
("scissors","lizard"): "scissors decapitated %s lizard",
("lizard","paper"): "lizard ate %s paper",
("paper","spock"): "paper disproved %s spock",
("spock","rock"): "spock vaporized %s rock",
("rock","scissors"): "rock crushed %s scissors"
}
answer = 'You %s, %s '
cpu_win = (CPU_Choice,User_Choice1)
winner = 'user'
if cpu_win in Who_Wins1:
winner = 'cpu'
answer += Who_Wins1[cpu_win]
else:
answer += Who_Wins1[cpu_win[::-1]]
return answer % wins[winner] % machine
You should also replace this:
User_Choice1 = input("Choose rock, paper, scissors, lizard or spock ")
with:
User_Choice1 = User_Choice()
Answered By - nokla
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.