Reputation:
We are assigned to make a rock, paper, scissor, lizard, spock game at school (introduction to programming). I am having a problem with getting my code to print the text that I have in def handResult. The problem is:
if I have print(handResult(playerHand, enemyHand)) on line 105 it will print for example "Rock crushes Lizzard. Player wins!" BUT it will also print "2" (because of the return function that I use so that it keeps score of the results).
If I don't have print(handResult(playerHand, enemyHand)) on line 105 it does not print the return function (0,1,2) but it also does not print the other text (""Rock crushes Lizzard. Player wins!")
so my question is: How do I get it to print the text (""Rock crushes Lizzard. Player wins!") but leave out the return number?
Sorry about the wonkey explanation - I am new to all of this so I don't know how to explain things properly.
Upvotes: 0
Views: 76
Reputation: 623
The problem is that you have a print
statement in the function handResult
! Each time you call the function, it prints out the result.
But you also have this line: print(handResult(playerHand, enemyHand))
.
That is the line that actually prints out the number, which is returned by the function.
A brutal way to solve that would be to remove the print in print(handResult(playerHand, enemyHand))
, (leaving only handResult(playerHand, enemyHand)
), but that is not optimal because a few lines above, in result = (handResult(playerHand, enemyHand))
, the program will still print out something (which is not desired).
The solution is to move the whole print structure above, and to remove the line containing print(handResult(playerHand, enemyHand))
, since the line result = (handResult(playerHand, enemyHand))
already prints what you need.
Code example below:
if playerHand in validHands:
# Selects random enemy hand
enemyHand = random.choice(validHands)
print(clearScreen)
print("Round " + repr(round))
print(separator)
print("Your hand: " + hands[playerHand - 1])
print("Enemy hand: " + hands[enemyHand - 1])
print("")
result = (handResult(playerHand, enemyHand))
# Present results
if result == 2:
pScore += 1
if result == +1:
eScore += 1
round += 1
print("")
printScore(pScore, eScore)
print(separator, flush = True) # Flush = true makes it so that it prints before sleeping
Upvotes: 2