[Tutor] Help!

Prasad, Ramit ramit.prasad at jpmorgan.com
Thu Sep 22 21:14:41 CEST 2011


From: tutor-bounces+ramit.prasad=jpmorgan.com at python.org [mailto:tutor-bounces+ramit.prasad=jpmorgan.com at python.org] On Behalf Of Joseph Shakespeare
Sent: Thursday, September 22, 2011 12:05 PM
To: tutor at python.org
Subject: [Tutor] Help!

Hello,

I am new Python (about 2 weeks) and need some help. I am making a rock paper scissors game that a user can play with the computer by using a while loop and if elif else statements. It needs to keep score of the amount of losses and wins, and give the user the option to play again after each round. I've tried so many different options and I don't know what else to try. My problem is making the program loop back around when the user selects y(yes) to play again and defining the variables at the beginning. Here's my program:

import random
print"Welcome to Rock,Paper, Scissors! This is a game of chance; the computer randomly picks one of three throws."
print""
print"Rock beats Scissors, but is beaten by Paper."
print"Scissors beat Paper, but are beaten by Rock."
print"Paper beats Rock, but is beaten by Scissors."
print""
print"r for Rock"
print"s for Scissors"
wins=0
loses=0
print"p for Paper"
player=raw_input("Please pick your throw: (r,s,p):")
computer= random.choice(['r','s','p'])
print "Computer throw:", computer
y="something"
play=y
while play==y:
    if player=='r':
        if computer== 'r':
            print "Tie! Throw again."
        elif computer=='s':
            print "You win! r beats s"
            wins=wins+1
        elif computer == 'p':
            print "You lose! p beats r"
            loses=loses+1
        else:
            pass
    elif player=='s':
        if computer== 's':
            print "Tie! Throw again."
        elif computer=='p':
            print "You win! s beats p"
            wins=wins+1
        elif computer == 'r':
            print "You lose! r beats s"
            loses=loses+1
        else:
            pass
    elif player=='p':
            if computer== 'p':
                print "Tie! Throw again."
            elif computer=='r':
                print "You win! p beats r"
                wins=wins+1
            elif computer == 's':
                print "You lose! s beats p"
                loses=loses+1
            else:
                pass
    else:
        print "Invalid entry"
    print""
    print"Game Summary"
    print"Wins:", wins
    print"Loses:",loses
    play=raw_input("Play again? y (yes) or n (no):"
print"Thanks for playing!"



Thanks!

Joey
===========================================================

Change 
> y="something"
TO :
> y="y"

Although, I would probably do something like
y=("y","yes")
play="y"
while play in y:



Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list