Random.randrange & counting

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2008
Posts: 6
Reputation: crackers is an unknown quantity at this point 
Solved Threads: 0
crackers crackers is offline Offline
Newbie Poster

Random.randrange & counting

 
0
  #1
Apr 11th, 2008
Im only new to python but im trying to write a code that will allow me to randomly generate a number between 1 & 15,
I know that part is
import random
def main():
number=random.randrange(15) + 1

But im not sure where I need to go to from here to enable me to:
1. prompt the user to enter a number between 1 & 15, then tell the user whether it is too high or low.
Im also trying to throw in a couple of random choices that allows me to have to random choices going at once but it won't always come back with two choices (hence random I guess!).
What I want is to be able to have a number of options available, keep a count on them (not sure how to write that one), and prompt the player when they reach a score on the game.
I know it probably sounds comfusing but any help would be great.
crackers
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 138
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Random.randrange & counting

 
0
  #2
Apr 11th, 2008
The best way to pick random integers between 1 and 15 is this way:
  1. import random
  2.  
  3. # selects one random integer from 1 to 15
  4. pick = random.randint(1, 15)
Then you set up one 'endless while loop' asking the user to enter the guess. Use series of if/elif statements comparing the guess with the pick. If the guess is higher or lower let the user know and stay in the loop. If the guess is correct break out of the loop and then tell the user that the guess is correct.
Last edited by bumsfeld; Apr 11th, 2008 at 1:36 pm. Reason: spelling
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 6
Reputation: crackers is an unknown quantity at this point 
Solved Threads: 0
crackers crackers is offline Offline
Newbie Poster

Re: Random.randrange & counting

 
0
  #3
Apr 12th, 2008
Thanks for that, what about if the randrange was to be words not integers?
say random.randrange(3) + 1
(So the range was to be melons, oranges and apples??) I want a user to input one option, so user enters melons and the computer responds with one of the other two options, then I want a score to be kept of who wins each round.

ANy idea's??

Thanks
Crackers
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 10
Reputation: Shakis is an unknown quantity at this point 
Solved Threads: 0
Shakis Shakis is offline Offline
Newbie Poster

Re: Random.randrange & counting

 
0
  #4
Apr 13th, 2008
  1. from random import *
  2.  
  3. data = ["apples","orange","melon"]
  4. data2=[]
  5. playerchoice= raw_input("enter your choice: ")
  6. for i in data:
  7. if playerchoice != i:
  8. data2 = data2 + [i]
  9. compchoice = data2[randint(0,2)]

that will get both choices you just need to do the compare bit i dont know what the winning conditions are
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Random.randrange & counting

 
0
  #5
Apr 13th, 2008
Give this a try:
  1. import random
  2.  
  3. data = ["apple", "orange", "melon", "pear"]
  4. # this picks one item from the list above
  5. print random.choice(data)
Last edited by Ene Uran; Apr 13th, 2008 at 1:47 pm.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 6
Reputation: crackers is an unknown quantity at this point 
Solved Threads: 0
crackers crackers is offline Offline
Newbie Poster

Re: Random.randrange & counting

 
0
  #6
Apr 13th, 2008
Thanks guys, thats helped me out heaps. Not too many more questions. I have a prac which I know how to roll the dice to get the number, but how do I write it so that it keeps a count of the roll of the dice so that when the total of the dice rolled is equal to 15, it will then tell me how many times it took to roll that?
My Python for Absolute Beginners isn't helping me too much!!
Thanks again
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC