In this Python tutorial, learn to define and execute some intermediate to advanced Python functions by creating a python dice game example. In this Python tutorial, I am running Python IDLE (Python GUI) version 3.7.2
Within the Python functions, one will be able to pass data, which are known as parameters and the function will return the result as data.
Python Rolling Dice Game Functions
- Ask user for input of name
- Create virtual names of the second player to be chosen at random on each play
- Virtual Mars
- Virtual Jupiter
- Virtual Neptune
- Virtual Saturn
- Virtual Earth
- Virtual Venus
- Virtual Uranus
- Virtual Mercury
- Create variables of the dice that are random number generators – min number of 1 and max number of 6
- if/elif statements for equal, greater and larger than to validate the winner of the game
- while statement to keep the game active until the player decided to stop playing
Import Python Packages
1 2 3 | from sys import exit import random import time |
Range Number Variables
1 2 3 4 5 | min = 1 max = 6 x = 0 y = 0 |
User Name Input and Upper Case Name Variable
1 2 | player_name = input("Enter your name please: ") upper_name = player_name.upper() |
Create virtual second player names (Planet Names)
1 2 | virtual_names = ["Virtual Mars", "Virtual Jupiter", "Virtual Neptune", "Virtual Saturn", "Virtual Earth", "Virtual Venus", "Virtual Uranus", "Virtual Mercury"] |
Random name selection for second player
1 2 3 | for name in random.sample(virtual_names, 1): virtual_name = name virtual_name = virtual_name.upper() |
while Loop to Keep Playing or Exit
- Keep Playing = yes
- Stop Playing = no and exit the game
- Pick x and y random number
- Define the dice number from the min and max variables
- if statement to validate who wins or a draw for the game
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | roll_again = "yes" while roll_again == "yes": if roll_again == "no": exit(0) print time.sleep(1) print ("\nYour Turn") time.sleep(1) print print ("\nDie is rolling...") time.sleep (2) x = random.randint(min, max) y = random.randint(min, max) def dice (d): if d == 1: print (""" _________ | | | | | * | | | |_________| """) elif d == 2: print (""" _________ | | | * | | | | * | |_________| """) elif d == 3: print (""" _________ | | | * | | * | | * | |_________| """) elif d == 4: print (""" _________ | | | * * | | | | * * | |_________| """) elif d == 5: print (""" _________ | | | * * | | * | | * * | |_________| """) elif d == 6: print (""" _________ | | | * * | | * * | | * * | |_________| """) dice (x) time.sleep(1) print print ("\n") print (virtual_name + "'s", ("Turn")) time.sleep(1) print print ("\nDie is rolling...") time.sleep(2) dice(y) time.sleep(1) if x == y: print print ("\nIt's a draw.") time.sleep(1) print print ("\nInput 'yes' to play again or 'no' to stop playing.") print elif x > y: print print ("\nYou Won, Congratulations!"), upper_name time.sleep(1) print print ("\nInput 'yes' to keep playing OR 'no' to stop playing.") print elif x < y: print print (f"\nSorry! You Lost. Try again {upper_name}") time.sleep(1) print print ("\nInput 'yes' to keep playing OR 'no' to stop playing.") print roll_again = input(f"\nWould you like to roll the die again, {upper_name}? ") |
Python Dice Game Output
The below is the output using the Python IDLE to execute the above code combined.
I hope this Python tutorial on creating a a rolling dice game was helpful.