| | |
Random.randrange & counting
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2008
Posts: 6
Reputation:
Solved Threads: 0
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
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
The best way to pick random integers between 1 and 15 is this way:
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.
python Syntax (Toggle Plain Text)
import random # selects one random integer from 1 to 15 pick = random.randint(1, 15)
Last edited by bumsfeld; Apr 11th, 2008 at 1:36 pm. Reason: spelling
Should you find Irony, you can keep her!
•
•
Join Date: Apr 2008
Posts: 6
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Apr 2008
Posts: 10
Reputation:
Solved Threads: 0
Python Syntax (Toggle Plain Text)
from random import * data = ["apples","orange","melon"] data2=[] playerchoice= raw_input("enter your choice: ") for i in data: if playerchoice != i: data2 = data2 + [i] 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
Give this a try:
python Syntax (Toggle Plain Text)
import random data = ["apple", "orange", "melon", "pear"] # this picks one item from the list above print random.choice(data)
Last edited by Ene Uran; Apr 13th, 2008 at 1:47 pm.
drink her pretty
•
•
Join Date: Apr 2008
Posts: 6
Reputation:
Solved Threads: 0
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
My Python for Absolute Beginners isn't helping me too much!!
Thanks again
![]() |
Other Threads in the Python Forum
- Previous Thread: finding matching words in a file then coundted
- Next Thread: loops and variables
| Thread Tools | Search this Thread |
abrupt ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog decimals dictionaries dictionary directory drive dynamic error examples excel exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyqt python random recursion schedule scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable ventrilo wikipedia windows write wxpython xlib






