How to resolve a NameError in my 'rock, paper, scissors' function
I just started programming in python 2.7.x. and had to put a programm for
rock, paper and scissors into a function. In the programm itself I asked
the user for an input and now I have to change it to just using parameters
only and using return instead of 'print'. Its a simple piece of code but I
just keep getting the NameError: name 'x' is not defined.
So I run the function into shell and then I type in: rps(rock, paper).
Some hints on how to resolve this or advice on my code would be great.
Here's the code.
def rps(player1, player2): if (player1 != 'rock' and player1 != 'scissors'
and player1 != 'paper'): return 'This is not a valid object selection'
if (player2 != 'rock' and
player2 != 'scissors' and
player2 != 'paper'):
return 'This is not a valid object selection'
if (player1 == 'rock' and
player2 == 'scissors'):
return 'Player 1 wins.'
if (player1 == 'rock' and
player2 == 'rock'):
return 'There is a tie.'
if (player1 == 'rock' and
player2 == 'paper'):
return 'Player 2 wins.'
if (player1 == 'scissors' and
player2 == 'scissors'):
return 'There is a tie.'
if (player1 == 'scissors' and
player2 == 'rock'):
return 'Player 2 wins.'
if (player1 == 'scissors' and
player2 == 'paper'):
return 'Player 1 wins.'
if (player1 == 'paper' and
player2 == 'rock'):
return 'Player 1 wins.'
if (player1 == 'paper' and
player2 == 'paper'):
return 'There is a tie.'
if (player1 == 'paper' and
player2 == 'scissors'):
return 'Player 2 wins.'
No comments:
Post a Comment