Ask your own question, for FREE!
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW) 15 Online
OpenStudy (anonymous):

Hi, I am starting the course and was attempting the first assignment. I am stuck on the "Rock, Paper, Scissors" program. I managed to get the minimum I think they wanted (i.e. it told me who won, and would respond if there was an invalid selection). However, I wanted the program to stop if Player 1 gave anything other than rock, paper, or scissors. I could not figure out how to do that. I do not have enough room to post my code, but if anyone could share how to do what I described above please do! Thanks!

OpenStudy (anonymous):

p1 = raw_input("Player 1? ") p1 = p1.lower() if p1.lower() != 'rock' or\ 'paper' or\ 'scissors': print 'That is not a valid object selection' else: p2 = raw_input("Player 2? ") p2 = p2.lower() if p2.lower() != 'rock' or \ 'paper' or \ 'scissors': print 'That is not a valid object selection' if p1 == 'rock' and\ p2 == 'rock': print 'The players tie' elif p1 == 'rock' and\ p2 == 'paper': print 'Player 2 wins.' elif p1 == 'rock' and\ p2 == 'scissors': print 'Player 1 wins.' elif p1 == 'paper' and\ p2 == 'rock': print 'Player 1 wins.' elif p1 == 'paper' and\ p2 == 'paper': print 'The players tie.' elif p1 == 'paper' and\ p2 == 'scissors': print 'Player 2 wins' elif p1 == 'scissors' and\ p2 == 'rock': print 'Player 2 wins.' elif p1 == 'scissors' and\ p2 == 'paper': print 'Player 1 wins.' elif p1 == 'scissors' and\ p2 == 'scissors': print 'The players tie.'

OpenStudy (anonymous):

You can use a single regular expression pattern to check for multiple valid values. here's an example: >>> prog = re.compile('^paper|rock|scissors$',re.IGNORECASE) >>> prog.search('Rock')==None False >>> prog.search('potato')==None True >>>

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!