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

Recommended Answers

All 5 Replies

The best way to pick random integers between 1 and 15 is this way:

import random

# selects one random integer from 1 to 15
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.

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

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:

import random

data = ["apple", "orange", "melon", "pear"]
# this picks one item from the list above
print random.choice(data)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.