#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    8
    Rep Power
    0

    Little help please.


    Hi,

    New to the forums, i started a University coarse this year, I'm in my 30's and learning programming for the first time.

    I'm trying to get a program running in python 2.75, and it works fine, but I've been asked to do something in the program that i have tried to do but to no avail.

    It's a game of rock,paper scissors, like i say it works fine, but the only thing I'm not sure how to do is the following.

    In the event of a tie game (example CPU and Player pick the same..... Paper, paper for example). I need to get the program to return to the beginning, without being prompted to do so.

    How do i do this please?

    this is my code so far, any help would be great thanks......


    import random

    def main(str):
    a=random.randint(1,3)
    b=raw_input("What is your choice? Rock,Paper or Scissors\n"
    ">>>")

    if a==1:
    print " >>>Computer picks Rock"
    if a==2:
    print ">>>Computer picks Paper"
    if a==3:
    print ">>>Computer picks Scissors"
    if b=='rock'and a==1 :
    print ">>>Draw - Rock ties with Rock"
    if b=='rock'and a==2:
    print ">>>Computer wins - Paper wraps Rock "
    if b=='rock'and a==3:
    print ">>>You win - The Rock smashes Scissors"
    if b=='paper'and a==1:
    print ">>>You win - Paper wraps Rock"
    if b=='paper'and a==2:
    print ">>>Draw Paper ties with Paper"
    if b=='paper'and a==3:
    print ">>>Computer wins - Scissors cuts Paper"
    if b=='scissors'and a==1:
    print ">>>Computer wins - The Rock smashes Scissors"
    if b=='scissors'and a==2:
    print ">>>You win - Scissors cuts Paper"
    if b=='scissors'and a==3:
    print ">>>Draw - Scissors ties with Scissors"


    c=raw_input("Would you like to play again? y=Yes n=No Enter\n"
    ">>>")
    if c == "y":
    main(str)
    else:
    exit()
    return;

    d=raw_input('Would you like to play "Rock Paper Scissors?" y=Yes n=No ENTER\n'
    ">>>")
    if d=="y":
    main(str)
    else:
    exit()

    while true:
    if d=="y":
    main(str)
    else:
    break
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    Thank you for posting an effort. Better first try than most! Please see my signature about code posts in this forum. Please compare this modified working version with yours. Maybe you'll glean some insight. Maybe you'll laugh at me.
    Code:
    import random
    
    def main():
        a=random.randint(1,3)
        answer=raw_input("What is your choice? Rock,Paper or Scissors\n"
                    ">>>")
        b = answer.lower()  # make sure case doesn't matter for the answer.  Especially since you mixed case 'Rock' as example, but compare against 'rock'
    
        if a==1:
            print " >>>Computer picks Rock"
        elif a==2:
            print ">>>Computer picks Paper"
        else:  # a must be 3!
            print ">>>Computer picks Scissors"
    
        if b=='rock'and a==1 :
            print ">>>Draw - Rock ties with Rock"
        elif b=='rock'and a==2:
            print ">>>Computer wins - Paper wraps Rock "
        elif b=='rock'and a==3:
            print ">>>You win - The Rock smashes Scissors"
        elif b=='paper'and a==1:
            print ">>>You win - Paper wraps Rock"
        elif b=='paper'and a==2:
            print ">>>Draw Paper ties with Paper"
        elif b=='paper'and a==3:
            print ">>>Computer wins - Scissors cuts Paper"
        elif b=='scissors'and a==1:
            print ">>>Computer wins - The Rock smashes Scissors"
        elif b=='scissors'and a==2:
            print ">>>You win - Scissors cuts Paper"
        elif b=='scissors'and a==3:
            print ">>>Draw - Scissors ties with Scissors"
        else:  # the elif gives us an error test, and is computationally efficient.
            print "whoops!  can't parse that choice"
        
    
    while True:                         # True.  Python is case sensitive.
        d=raw_input('Would you like to play "Rock Paper Scissors?" y=Yes n=No ENTER\n'
                    ">>>")
        if d.lower() != "y":
            break
        main()
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo