Hey, I'm new to Python programming and I've been making a simple rock paper scissors program for practice. I've managed to get it working; it asks the player to enter 'a' for rock, 'b' for scissors or 'c' for paper. If the player entered a, b or c, a number is generated to determine the opponents choice and then the two choices are compared to see if the player won, lost or drew. If the player pressed none of the right keys, then the player is asked to enter again.

The problem with this is that the program asks the player what they want to choose, then often asks again before printing win, lose or draw, then asks another 2 or 3 times before ending, even if they put in a valid letter. Can anyone help?

import random

def rpsmessageone(pchoice):		#Message that is returned to the player upon choosing an item, returns 0/invalid if a b or c are not pressed
	if pchoice == "a":
		print "You chose rock!"
		valid = 1
		return valid
	elif pchoice == "b":
		print "You chose scissors!"
		valid = 1
		return valid
	elif pchoice == "c":
		print "You chose paper!"
		valid = 1
		return valid
	else:
		print "Invalid Letter!"
		valid = 0
		return valid

def rpschoose():		#Function to ask the player what they want to choose, takes their input and outputs it to other functions
	"""This allows the player to choose his or her item, rock, paper or scissor"""
	plyerchoice = raw_input("Press \n\t'a' for rock, \n\t'b' for scissors \n\t'c' for paper\n")
	return plyerchoice

def rpsmain():		#Centralised function, to run the other two
	plyerchoice = rpschoose()
	valid = rpsmessageone(plyerchoice)
	return valid

valid = rpsmain()
while valid == 0:		#Runs the main function until player presses a valid key(a,b,c)
	valid = rpsmain()


aichoice = random.randint(0,4)

if aichoice == 1 and rpschoose() == "a":
	print "Your opponent chooses rock!\nDraw!"
if aichoice == 1 and rpschoose() == "b":
	print "Your opponent chooses rock!\nYou lose!"
if aichoice == 1 and rpschoose() == "c":
	print "Your opponent chooses rock!\nYou win!"

if aichoice == 2 and rpschoose() == "a":
	print "Your opponent chooses scissors!\nYou win!"
if aichoice == 2 and rpschoose() == "b":
	print "Your opponent chooses scissors!\nDraw!"
if aichoice == 2 and rpschoose() == "c":
	print "Your opponent chooses scissors!\nYou lose!"
	
if aichoice == 3 and rpschoose() == "a":
	print "Your opponent chooses paper!\n You lose!"
if aichoice == 3 and rpschoose() == "b":
	print "Your opponent chooses paper!\nYou win!"
if aichoice == 3 and rpschoose() == "c":
	print "Your opponent chooses paper!\nDraw!"

Recommended Answers

All 2 Replies

It works now, thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.