1 Million Sided Dice - Python Basics Series - Lesson 1

in #python7 years ago (edited)

Lesson 1: Understanding the basics (Coding experience required: Minimal to none)

What will I learn:

  • Importing of Librarys
  • Loops (While and For)
  • Input and print statements
  • Different between = and ==
  • How to create random numbers

I have created a RPG dice roller in Python. You can roll any amount of sided dice any amount of times.

If you want to try the program on your computer at home please follow this easy to follow Python setup video. https://www.youtube.com/watch?time_continue=8&v=0y5XlNeFxNk

SOURCE CODE - RPG DICE

import random


def main():
    again = "y"
    while again == "y":
        diceSides = int(input("How many sides does your dice have? "))
        rolls = int(input("How many time do you want to roll your dice? "))
        for x in range(1, rolls+1):
            randomNumber = random.randint(1, diceSides)
            print(str(x)+"> You Rolled a", diceSides , "sided dice and rolled a", randomNumber)

        print("\n")
        again = input("Roll some more?")


main()

Use cases

This program could be used in place of regular dice at a game of pen and paper D&D when you need to keep the noise down.

  • Pros: These dice are free, have as many sides as you like, you can't lose them or drop them on the floor.
  • Cons: Don't look as cool, can't feel, can't touch, wife may accuse you of cheating saying you messed with the code if she loses the game, laptops and PCs are heavier than dice (no mobile version yet)

As you see, its a small basic program. Don't worry if you don't understand it, I will teach you what the code does and we can learn together. The idea is that in future posts keep ramping up the coding and learning until we have made a basic game but we may divert to fun little programs along the way to test logic and code together :)

Also, if you haven't read it, please read my previous post which is a story about and why I originally made this program 25 years ago in QBASIC: https://steemit.com/programming/@jimpew/re-learning-to-program-at-40-the-early-years

Here is a break down of the code:

import random

The above code imports the random library / object that has lots of classes / functions that we can use in or program. Any program that uses random number generation would need this library.

def main()

The def main basically just means we are saying anything indented in the code below it is part of the main function and all variables set with the scope of the indent only belong to this function. Point to note. I could have called it def banana() or anything else :)

 again = "y"
    while again == "y":

The above code set again to "y" and sets off the loop. It will keep looping around the loop indentation until the variable again is set to anything but "y". Point to note: You must use double == when comparing a variable and a single = when setting one. First I set again to "y" using = then compare using the double == at the start of the loop.

        diceSides = int(input("How many sides does your dice have? "))
        rolls = int(input("How many time do you want to roll your dice? "))

The above code asks the user within the loop how many sides, how many times to roll. This uses 2 input statements that set Integer numbers (ie a whole number not 3.4 just 3) to the variables diceSides and rolls.

        for x in range(1, rolls+1):
            randomNumber = random.randint(1, diceSides)

Okay, the code above is a loop within a loop, commonly called a nested loop, because its sitting pretty within a loop. This is a for loop and you can set how many times you want it to loop. In this case we want how ever many times the user inputs in rolls. +1
Next Line we set randomNumber to = a random integer between 1 and how many sides the user asks for. This would not work with out the import random at the start. the randint after the . is just a function within the random library we imported at the start. There are more functions that belong to the random that we may cover in later lessons.

Question of the Lesson

       print(str(x)+"> You Rolled a", diceSides , "sided dice and rolled a", randomNumber)

The next line is print(str(x)+"> You Rolled a", diceSides , "sided dice and rolled a", randomNumber) This a question to the class: What would be printed using this print function if the user answers question 1: 20 and question 2: 2?

Answers in reply below please.

        print("\n")
        again = input("Roll some more?")
        if again != "y":
            again = "n"

We are now outside the nested loop and in the code above we print a new line. So just like pressing enter on the keyboard. Then we ask for input in the again variable..... if anything but "y" is entered the program will exit. We don't need an if statement as the while loop is already checking that again == "y" anything else the user inputs will exit the loop end the program. The Main() at the last line is running the function. With out it the Main() function would never run!

I hope you enjoyed it. Why not install install python (links below) and copy and paste my code.

Things to try:

  • Please try and break the program. Please try and see how high you can go with dice.
  • I know 1 million works but can you go much higher. What is the limit?
  • What happen if you input characters instead of number :)

There will be more to come! Any comments and suggestions are more than welcome. Also, please point out any typos or mistakes so I can fix them

Please don't forget to follow, upvote and resteem if you enjoyed this lesson.

Thank you learning Python with me. Class dismissed.

Quote of the day:

Knowledge is Power

Refs:

Dice Pic ► https://imgur.com/gallery/prNzd
Python ► https://www.python.org/downloads/
Python IDE ► https://www.jetbrains.com/pycharm/download/#section=windows

My links:

Support ► https://www.patreon.com/JimPew
Donate ► https://twitch.streamlabs.com/jim_pugh
Bitcoin Wallet address ► 1Kznn8JHGoZUGgbXWhGoAWffDnvXnqn4AR
Subscribe ► https://www.youtube.com/c/BrothersofGamingBoG?sub_confirmation=1
Youtube ► https://www.youtube.com/c/BrothersofGamingBoG
Youtube Play Lists ► https://www.youtube.com/user/jimpugh77/playlists
Twitter ► https://twitter.com/BrosofGamingBOG
Twitch ►

/

Sort:  

Cons: Don't look as cool, can't feel, can't touch, laptops and PCs ate heavier than dice (no mobile version yet)

One more cons: if I play with the wife and she losses the game, they she is sure I'm cheating through the code ;-)

That actually happened... well, not your wife but a teen boy added +2 to a 20 sided dice and cheated. If you read my relearning to program at 40 blog before this one. 'Malc the Calc' put a stop to it by making it into an EXE. Ill add your wife to my list of cons.

Truce: don't mean to break up all this serious talk but um, I wish i had a million sided cheesecake, mmm im hungry lol

Truce's inner mind: aye fool what you doing posting that, aint no body gonna laugh at that, delete it NOW!

Truce: yo chill bro, i think its funny.

Truce inner mind: Yeah you and only you, get off this page. and hey, HEY pay attention, get off this computer and go do somthing constructive.

Truce: no, im not a construction worker.

Truce inner mind: (sigh) I give up.

Would. You. Like. A plate. You know. For your cake.

Coin Marketplace

STEEM 0.21
TRX 0.25
JST 0.038
BTC 95358.83
ETH 3366.72
USDT 1.00
SBD 3.11