Python rock paper scissors game help needed!

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Python rock paper scissors game help needed!

Post by lammer on Thu Jan 15, 2009 8:37 am
([msg=16300]see Python rock paper scissors game help needed![/msg])

I read in one of the posts how you should while learning a programming language start creating a project..... i decided to create a rock paper scissors game, but i have a little problem, i don't know how to command the computer to choose one of the 3 things. for instance: if the user chooses paper how can i command the computer to either choose rock, paper or scissors having all of them have a 33% chance of being chosen?


this is what i came up with (in this game you always loose =) and u cant win hehe)

Code: Select all
# Rock paper scissor game


from time import sleep

print "Please select: "
print "1  Rock"
print "2  Paper"
print "3  Scissors"

# Get Player's choice

player = input ("Choose from 1-3: ")


# Decide what the computer will choose

if player == 1:
    print "You choose Rock"
    sleep (2)
    print "CPU chooses Paper"
    print "You loose, and you will never win!"

elif player == 2:
    print "You choose Paper"
    sleep (2)
    print "CPU chooses Scissors"
    print "You loose, and you will never win!"

else:
    print "You choose Scissors"
    sleep (2)
    print "CPU chooses Rock"
    print "You loose, and you will never win!"
The Mentor---You may stop this individual, but you can't stop us all... after all, we're all alike.
lammer
New User
New User
 
Posts: 4
Joined: Sun Jan 11, 2009 11:23 am
Blog: View Blog (0)


Re: Python rock paper scissors game help needed!

Post by comperr on Thu Jan 15, 2009 8:48 am
([msg=16302]see Re: Python rock paper scissors game help needed![/msg])

This might help
Code: Select all
import random
print random.random()
random.randint(1,3)
User avatar
comperr
Poster
Poster
 
Posts: 373
Joined: Mon Apr 07, 2008 6:52 pm
Location: /dev/null
Blog: View Blog (0)


Re: Python rock paper scissors game help needed!

Post by lammer on Thu Jan 15, 2009 9:19 am
([msg=16304]see Re: Python rock paper scissors game help needed![/msg])

thx ill try that

-- Thu Jan 15, 2009 9:58 am --

What operation does 'random.random ()' execute??

when i run the script with it it shows a number like this 0.17830951 !!

now im stuck here (this is not where the script begins)

Code: Select all

if player == 1:
    print "You choose Rock"
    sleep (2)
    print "CPU chooses", random.radint(1,3)
    sleep (.5)
   
if random.radint == player:
    print "You tie, try again!"
elif random.radint == 2:
    print "You loose, and you will never win!"
elif random.radint == 3:
    print "You win!!"



when i run the script i get an error message saying:

Code: Select all
Traceback (most recent call last):
  File "C:\Users\USER\Desktop\PYTHON PROJECTS\rockpaperscssorsRANDOM.py", line 23, in <module>
    print "CPU chooses", random.radint(1,3)
AttributeError: 'module' object has no attribute 'radint'
The Mentor---You may stop this individual, but you can't stop us all... after all, we're all alike.
lammer
New User
New User
 
Posts: 4
Joined: Sun Jan 11, 2009 11:23 am
Blog: View Blog (0)


Re: Python rock paper scissors game help needed!

Post by Defience on Wed Jan 21, 2009 2:09 pm
([msg=16734]see Re: Python rock paper scissors game help needed![/msg])

That means that your program cannot find a module with the name 'radint' and that's because you've misspelled it.....it's 'randint'.
http://docs.python.org/library/random.html
User avatar
Defience
Addict
Addict
 
Posts: 1280
Joined: Thu Jun 12, 2008 3:16 pm
Blog: View Blog (0)


Re: Python rock paper scissors game help needed!

Post by Arrexel on Fri Feb 20, 2009 3:29 pm
([msg=18312]see Re: Python rock paper scissors game help needed![/msg])

comperr wrote:This might help
Code: Select all
import random
print random.random()
random.randint(1,3)


If I'm not mistaken, this would generate a random number of which is then printed to the console. This number would be a number between 0 and 1, with 12 decimal places, unless it is 0 or 1. The selection I would choose would be to use
Code: Select all
random.randint(1,3)
as this would choose a whole number - either 1, 2 or 3. Although the first statement would work if you used another statement to check if it is between a certain range, the bottom one is the best choice.

I sort of zoned out at the beginning of the first line, and forgot what I thought when I saw that... I think you just used that as an example, but this person may not have been sure which to use. The two statements would also generate two different numbers, which may cause confusion to somebody new to variable definition.

Sorry if I seem like a dick or pointing out the obvious, as I am not fully 'here,' as I am quite tired from being up almost all night.

Good luck,

-Arr
"I think there is a world market for maybe five computers."
Unverified Quote, 1945 - Thomas J. Watson, Founder of IBM
User avatar
Arrexel
New User
New User
 
Posts: 31
Joined: Fri Feb 13, 2009 7:44 pm
Location: Ontario, Canada
Blog: View Blog (0)


Re: Python rock paper scissors game help needed!

Post by NamelessNr1 on Fri Feb 20, 2009 5:34 pm
([msg=18315]see Re: Python rock paper scissors game help needed![/msg])

There should be a round() function in python. I don't really know but you could look it up.
NamelessNr1
New User
New User
 
Posts: 5
Joined: Sun Jan 11, 2009 10:25 am
Blog: View Blog (0)


Re: Python rock paper scissors game help needed!

Post by Arrexel on Fri Feb 20, 2009 7:20 pm
([msg=18324]see Re: Python rock paper scissors game help needed![/msg])

There is a method of rounding, but rounding to either 0 or 1 would only give two results, not the required 3 to run this program.
"I think there is a world market for maybe five computers."
Unverified Quote, 1945 - Thomas J. Watson, Founder of IBM
User avatar
Arrexel
New User
New User
 
Posts: 31
Joined: Fri Feb 13, 2009 7:44 pm
Location: Ontario, Canada
Blog: View Blog (0)


Re: Python rock paper scissors game help needed!

Post by kiddietron on Fri Feb 20, 2009 7:38 pm
([msg=18328]see Re: Python rock paper scissors game help needed![/msg])

I think there's a formula like

--This is java code
int randInt = ((int)Math.random()*a+b);

the variable a would be the range of which you wish the numbers to go (exclusive)
the variable b would shift the starting number from 0 to some other number.

int randInt = ((int)Math.random()*5+1); --This should print a random number between 1-5.

I think Rock Paper Scissors is a great starting program to learn some i/o stuff in a new language. :) Tic-tac-toe and John Conway's game of life is good too.
--Advertising's a no-no. :)
kiddietron
Experienced User
Experienced User
 
Posts: 57
Joined: Sat Aug 16, 2008 8:17 pm
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests