Total beginner question - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Total beginner question (/thread-2708.html) |
Total beginner question - AngelForge - Apr-04-2017 Hi, first of all, I hope I'm in the right section of the forum. I decided to learn a programming language and some Youtube videos later, I decided to go for Python. As a child I used to play around with basic on the C64 and I had some contact to free pascal lately. So, I think the best way to start for me is to play around with Python. Write little programs for fun like I did back when I was a child. So, here is my problem. I want to generate random numbers, to simulate a dice roll, for example. I the Python 3.6.0 documentation I found: 9.6. random - Generate pseudo-random numbers I read through it and tried out various things but I always get error messages. My goal was to have a variable get a random value. So, how is that done? Can someone help me with that? Thank you! :-) Sven RE: Total beginner question - wavic - Apr-04-2017 from random import randint result = randint(1, 6)This will return a random integer between 1 and 6 Ref: https://docs.python.org/3/library/random.html#random.randint RE: Total beginner question - AngelForge - Apr-04-2017 Thank you for your answer, Wavic! :-) Do I understand it correcly, that I have to use the line: from random import randint (a,b)only once at the start of the program and can use it multiple times then? And if I like to use other option, I just add them like: from random import random ? RE: Total beginner question - wavic - Apr-04-2017 random is the module which you import or import a class, method from it. Yes you can do for another method the same or for few of them separated with a comma. That way you import only what you need instead of the whole module You can use it like this too import random # module method # | | # | | result = random.randint(1, 6)You import here the whole random module and can use all of its classes and methods: module.method(), module.class.method() https://python-forum.io/Thread-Basic-Modules-part-1 https://python-forum.io/Thread-Basic-Modules-part-2 https://python-forum.io/Thread-Basic-Modules-part-3 RE: Total beginner question - AngelForge - Apr-04-2017 Thank you, especially for the extra explanation. :-) RE: Total beginner question - Larz60+ - Apr-04-2017 You may also like to look at what others have done, see: https://pypi.python.org/pypi?%3Aaction=search&term=dice+roll&submit=search RE: Total beginner question - Low_Ki_ - Apr-04-2017 Heres something I've come up with when it comes to dice rolls... Hope it helps. from random import randint class Die: def __init__(self, sides, roll=0): self.sides = sides self.roll = roll def roll_die(self): if self.sides==6: while self.roll < 6: print("You roll a six sided die: " + str(randint(1, 6))) self.roll += 1 elif self.sides==10: while self.roll < 6: print("You roll a ten sided die: " + str(randint(1, 10))) self.roll += 1 elif self.sides==20: while self.roll < 6: print("You roll a twenty sided die: " + str(randint(1, 20))) self.roll += 1 else: print("Mistakes have been made!") six_sided_die = Die(6) six_sided_die.roll_die() print('') ten_sided_die = Die(10) ten_sided_die.roll_die() print('') twty_sided_die = Die(20) twty_sided_die.roll_die() RE: Total beginner question - ichabod801 - Apr-04-2017 I don't think that's what he's really looking for. Your Die object rolls six times at once, and then won't roll again. Something simpler and more to the point would be: import random class Die(object): def __init__(self, sides = 6): self.sides = sides self.roll() def roll(self): self.up_face = random.randint(1, 6) return self.up_face Or even simpler:import random def roll_die(sides = 6): return random.randint(1, sides)
RE: Total beginner question - Low_Ki_ - Apr-08-2017 Oh i see now. Because I assign 0 to roll then use a while loop saying while roll < 6... He is looking for a re-roll object. I did this example just to show the random.randint() method being repeated for practice. |