6 Replies - 1855 Views - Last Post: 16 February 2013 - 07:50 PM Rate Topic: -----

#1 The Dark Lord of Algebra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-February 13

Trying to code a simple paper, scissors, stone game

Posted 04 February 2013 - 03:16 PM

I have got as far as being able to input 'paper', 'scissors' or 'stone'. It's then that I get an error message saying that whatever I have inputed is "undefined". Here's my code:
[#the computer will select a random number from 1 to 3
#first the draws ...
#then the player wins ...
#then the computer wins ...
#then invalid entries
#then use up one round

while rounds != rounds_required:
    mighty = random.randint(1, 3)
    choice = input("What'sss your choisssce? ")
    if choice == "paper" and mighty == 1 or choice == "scissors" and mighty == 2 or choice == "stone" and mighty == 3:
        print "Amazzzingly you and the Mighty Python think alike ... "
        rounds = rounds + 1
    elif choice == "scissors" and mighty == 1:
        print "Aaah, your scissorsss cut my Mighty paper !!"
        player = player + 1
        rounds = rounds + 1
    elif choice == "stone" and mighty == 2:
        print "Youch, your pesssky ssstone blunted my Mighty ssscissorsss !!"
        player = player + 1
        rounds = rounds + 1
    elif choice == "paper" and mighty == 3:
        print "Hisss ... my Mighty ssstone wasss wrapped by your creepy paper !!"
        player = player + 1
        rounds = rounds + 1
    elif choice == "scissors" and mighty == 3 or choice == "paper" and mighty == 1 or choice == "stone" and mighty == 2:
        print "Hah! The Mighty Python triumphsss!!!"
        python = python + 1
        rounds = rounds + 1
    else:
        print "Invalid attempt ... please type all lower cassse and check your ssspelling."

#announce the final scores

if player == python:
    print "You were a worthy challenger we both scored", player," the true winner wasss the game."
elif player > python:
    print "I sssalute your random cunning you beat me by", player, " pointsss to", python, "."
else:
    print "The Mighty Python triumphsss by", python, " points to a mere", player, "pointsss!"]


I thought it might be the way I have herded together all those "and" and "or" commands but I rewrote it as 9 different elifs and got the same outcome. Of course that doesn't necessarily mean that the "and"s and "or"s are any good themselves does it?

This post has been edited by modi123_1: 04 February 2013 - 03:18 PM
Reason for edit:: highlight the text and just click the 'code' button in the text editor


Is This A Good Question/Topic? 0
  • +

Replies To: Trying to code a simple paper, scissors, stone game

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: Trying to code a simple paper, scissors, stone game

Posted 04 February 2013 - 04:31 PM

I notice that you do this condition...

if player == python:



After you enter in a value into choice and it goes into one of the first if/elif statements, it won't set a value for python. It will skip over the condition where you set the python variable up. Thus you would be comparing player to a variable that doesn't exist (aka undefined).

See if that is what is happening. To fix it, you will need to setup the python variable before the while loop to some default value. Or make sure that no matter what if/elif it goes into, python gets set to something.

Hopefully that is what is wrong and it fixes it. :)/>

This post has been edited by Martyr2: 04 February 2013 - 04:32 PM

Was This Post Helpful? 1
  • +
  • -

#3 The Dark Lord of Algebra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-February 13

Re: Trying to code a simple paper, scissors, stone game

Posted 05 February 2013 - 12:24 PM

Thanks for the suggestion - you are right. The only problem is that, accidently, I didn't attach the first part of my code which was:
[#paper, scissors, stone
#1 = paper, 2 = scissors, 3 = stone
#set the base number of rounds
#set the base score for the player and the computer

import random

rounds = 0
player = 0
python = 0

#issue the computer's challenge
#ask how many rounds the user wants to play

print """Thisss isss the Mighty Python talking ...
You are challenged to a contessst of random cunning ...
The game isss called - Paper, Ssscissorsss, Ssstone ..."""
rounds_required = int(raw_input ("How many rounds are you brave enough to play? "))

#create a loop for the rounds until the chosen number of rounds has been reached]
Was This Post Helpful? 0
  • +
  • -

#4 The Dark Lord of Algebra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-February 13

Re: Trying to code a simple paper, scissors, stone game

Posted 05 February 2013 - 12:36 PM

(I just posted again because I think I now have the hang of the 'code' tags)

Thanks for the suggestion - you are right. The only problem is that, accidently, I didn't attach the first part of my code which was:

#paper, scissors, stone
#1 = paper, 2 = scissors, 3 = stone
#set the base number of rounds
#set the base score for the player and the computer

import random

rounds = 0
player = 0
python = 0

#issue the computer's challenge
#ask how many rounds the user wants to play

print """Thisss isss the Mighty Python talking ...
You are challenged to a contessst of random cunning ...
The game isss called - Paper, Ssscissorsss, Ssstone ..."""
rounds_required = int(raw_input ("How many rounds are you brave enough to play? "))

#create a loop for the rounds until the chosen number of rounds has been reached

Was This Post Helpful? 0
  • +
  • -

#5 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 117
  • View blog
  • Posts: 215
  • Joined: 11-January 13

Re: Trying to code a simple paper, scissors, stone game

Posted 05 February 2013 - 04:44 PM

You are using python 2.7 (as indicated by the print statements) but you used input.
You need to use raw_input instead.

Change the line where you define your choice variable to this:
choice = raw_input("What'sss your choisssce? ")


-Mek
Was This Post Helpful? 1
  • +
  • -

#6 The Dark Lord of Algebra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-February 13

Re: Trying to code a simple paper, scissors, stone game

Posted 06 February 2013 - 05:14 AM

Thanks Mekire - that was the problem. When you pointed it out I could not believe that I hadn't used raw_input until I looked back at my code. You have saved my muchos grief.
Was This Post Helpful? 0
  • +
  • -

#7 lisperati  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-February 13

Re: Trying to code a simple paper, scissors, stone game

Posted 16 February 2013 - 07:50 PM

#Rock Paper Scissors Game
from random import randint


chose = ["rock","paper","scissors"]

result = [[["Draw Try Again."],["Paper Covers Rock -- Human Wins"],["Rock Smashes Scissors -- Computer Wins"]],[["Paper Covers Rock -- Computer Wins"],["Draw Try Again"],["Scissors Cut Paper -- Human Wins"]],[["Rock Smashes Scissors -- Human Wins"],["Scissors Cut Paper -- Computer Wins"],["Draw Try Again"]]]

player = int(raw_input("Rock <0> Paper <1> Scissors <2>"))

computer = randint(0,2)



print "Player chose ",chose[player]," and computer chose ",chose[computer]

print result[computer][player]    



Just thought id say there is a better way to solve this problem. When you look at my code you will see that ive taken advantage of how the code repeats and put it all into a list for easy retrieval instead of using many complicated if thens. Also if you want to make this code repeat games just put the body into a while loop with and exit statement when the player wants to quit.

Also if you want to attach this to a GUI then all you have to do is call the library and then past this into the main loop and put the player responses into a text box or use a frame to do an animation.

Anyway i thought id show you an easy way to achieve the result you want.

Hope you like this.

Regards
Lisperati the lisper from the darkest reaches of the internet.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1