Enigma Group's Hacking Forum



User Info
Welcome, Guest. Please login or register.
May 18, 2012, 08:06:00 AM

Login with username, password and session length
Search:     Advanced search
News
Need a hash cracked? Use the Enigma Group Hash Cracker! It's the largest hash library on the interwebz.
Forum Stats
33912 Posts in 4171 Topics by 38418 Members
Latest Member: cbbyfhxcax
Enigma Group's Hacking Forum  |  General  |  Programming Help  |  Python  |  Trying to make a rock-paper-scissors-game (and failing)
« previous next »
Pages: [1] Print
Author Topic: Trying to make a rock-paper-scissors-game (and failing)  (Read 246 times)
Stuff
Newbie
*
Offline Offline

Posts: 22
  • Respect: 0

  • « on: January 28, 2012, 03:37:18 AM »
    0

    So, I am attempting to construct a rather simple rock-paper-scissors game in Python, and after several tries and fails I've reached this obstacle: Every time I input a choice, it always returns the inputerror(). Doesn't matter if it's 'rcok','paper' or 'scissors', the same thing happens always. Can anybody spot the flaw in the code?

    P.S. I know this is incomplete and fugly as hell, don't hate me on it. Also, I AM a complete newpie.

    Code: [Select]
    import random, time

    print "Loading..."
    #3>2>1>3
    rock = 1
    paper = 2
    scissors = 3
    draw = "Draw! Try again."
    print "Defining functions..."
    def inputerror():
        print ""
        print "Error: invalid input. Terminating..."
        time.sleep(4)
        exit()


    print '''

    This is a simple rock-paper-scissors-game.
    Type your choice in small letters and have fun!

    '''
    player_choice = raw_input("Enter your choice here: ")
    computer_choice = random.choice('123')
    int(computer_choice)

    if player_choice == scissors and computer_choice == 1:
        print "rock, you lose!"
    elif player_choice == scissors and computer_choice == 2:
        print "paper, you win!"
    elif player_choice == computer_choice:
        print draw
    elif player_choice == paper and computer_choice == 1:
        print "rock, you win!"
    elif player_choice == paper and computer_choice == 3:
        print "scissors,you lose!"
    elif player_choice == computer_choice:
        print draw
    elif player_choice == rock and computer_choice == 2:
        print "paper, you lose!"
    elif player_choice == rock and computer_choice == 3:
        print "scissors, you win!"
    elif player_choice == computer_choice:
        print draw
    elif player_choice != rock or paper or scissors:
        inputerror()
    Logged
    dark_void
    0100010001100001
    0111001001101011
    0010000001010000
    Veteran
    *******
    Online Online

    Posts: 1005
  • Respect: +7


  • « Reply #1 on: January 28, 2012, 03:52:08 AM »
    0

    I remember there an easter egg on eg that rock paper sicissors.
    Logged
    lol
    LetMeThink
    EnigmaClusive
    Sr. Member
    ***
    Online Online

    Posts: 315
  • Respect: +2


  • I'll meet you on the dark side of the moon

    « Reply #2 on: January 28, 2012, 04:14:25 AM »
    0

    I remember there an easter egg on eg that rock paper sicissors.
    8th i think
    Logged
    Politicians and diapers have one thing in common. They should both be changed regularly, and for the same reason.
    True story.
    Hypertext
    Newbie
    *
    Offline Offline

    Posts: 20
  • Respect: 0

  • « Reply #3 on: January 28, 2012, 06:40:35 AM »
    0

    I remember there an easter egg on eg that rock paper sicissors.
    8th i think
    No, its the 1st. I just did it today :)
    Logged
    I09KVSyiqFOlMJSxVUEbnKZu
    Stuff
    Newbie
    *
    Offline Offline

    Posts: 22
  • Respect: 0

  • « Reply #4 on: January 28, 2012, 11:40:23 AM »
    0

    Although I must say that your replies are both informative and enjoyable, I'd like to get an answer, if anyone is willing to use the time. Or was my op considered to be a troll(which it is not), in which case I of course should modify the post before presenting it unto your eyes.
    Logged
    Distorted
    Veteran Member
    Newbie
    ***
    Offline Offline

    Posts: 11
  • Respect: 0

  • </care>

    « Reply #5 on: January 28, 2012, 04:45:27 PM »
    0

    Okay I see a couple errors at the moment. Including your inputError one.

    I'll start with the error you asked first. At the end of you Elseif block you have this piece of code
    Code: [Select]
    elif player_choice != rock or paper or scissors:
        inputerror()

    This is incorrect, Since this statment will ALWAYS evaluate to TRUE. This is because the statement you have above is the same as this one.
    Code: [Select]
    elif player_choice != rock or 2 or 3: #remember you defined paper = 2 and scissors = 3:
        inputerror()

    And this Code is also the same as because any value besides 0 is considered True.
    Code: [Select]
    elif player_choice != rock or True or True:
       inputerror()


    What you probably meant to write was this:
    Code: [Select]
    elif player_choice != rock or player_choice != paper or player_choice != scissors:
       inputerror()

    Which sadly is also incorrect. Since you want to use the AND operator because you want to make sure the user inputed none of the options. That will fix this error. Now the next problem arises since you defined rock paper and scissors to be numerical values and the user is inputting a string. 

    So if player_choice == scissors and computer_choice == 1: doesn't really make sense since player_choice should be "rock" "paper" or "scissors" and the value of scissors is 2.


    I'll let you work on it some more since I don't wanna fix the whole thing for you.
    « Last Edit: January 28, 2012, 04:53:52 PM by Distorted » Logged
    Stuff
    Newbie
    *
    Offline Offline

    Posts: 22
  • Respect: 0

  • « Reply #6 on: February 03, 2012, 12:48:19 PM »
    0

    Thanks for your help and sorry for late answer (net was cut off for a few days, crappy modem). I'll try these out and check back in a few days.
    Logged
    Stuff
    Newbie
    *
    Offline Offline

    Posts: 22
  • Respect: 0

  • « Reply #7 on: February 07, 2012, 10:18:03 AM »
    0

    So, i fix'd some things and the current code is this:

    Code: [Select]
    import random, time

    print "Loading..."
    #3>2>1>3
    compwins = 0
    playerwins = 0
    draw = "Draw! Try again."
    print "Defining functions..."
    time.sleep(1) #gotta have decoration, y'know?
    def inputerror():
        print ""
        print "Error: invalid input. Terminating..."
        time.sleep(4)
        exit()
    def situation():
        print 'Your wins: ' #gives concat error when I try to print in one line
        print playerwins
        print 'Comp\'s wins: '
        print compwins


    print '''

    This is a simple rock-paper-scissors-game.
    Type in the numerical equivalent of your choice.
    rock = 1 ,paper = 2, scissors = 3
    You can see the current situation by typing 'situation()'

    Have fun!

    '''
    player_choice = raw_input("Enter your choice here: ")
    str(player_choice)
    computer_choice = random.choice('123')


    if player_choice == '3' and computer_choice == '1':
        print "rock, you lose!"
        compwins + 1
    elif player_choice == '3' and computer_choice == '2':
        print "paper, you win!"
        playerwins + 1
    elif player_choice == computer_choice:
        print draw
    elif player_choice == '2' and computer_choice == '1':
        print "rock, you win!"
        playerwins + 1
    elif player_choice == '2' and computer_choice == '3':
        print "scissors,you lose!"
        compwins + 1
    elif player_choice == computer_choice:
        print draw
    elif player_choice == '1' and computer_choice == '2':
        print "paper, you lose!"
        compwins + 1
    elif player_choice == '1' and computer_choice != 1 and computer_choice != 2: #Doesn't seem to work any other way
        print "scissors, you win!"
        playerwins + 1
    elif player_choice == computer_choice:
        print draw
    elif player_choice != '1' and player_choice != '2' and player_choice != '3' and player_choice != 'situation':
        inputerror()
    elif player_choice == 'situation':
        situation()


    It works fine, but the situation() function is pretty much obsolete because it has to restart in the start of every match. Maybe wrapping it up in a function would work? There are some stuff I couldn't figure out as shown in comments. I'm planning on adding some additional decoration in the program. (Gonna make million bucks with this). Opinion?
    Logged
    Pages: [1] Print 
    « previous next »
     

    Find Us on Facebook! Find us at Facebook! - Follow Us! Follow us with Twitter! - Make sure to Stumble us! Stumble upon us! - Subscribe! Subscribe to our feed!
    Review enigmagroup.org on alexa.com

    ©Enigma Technology Group Inc. 2005-2012