Code Club followup
So at yesterdays code club we were looking at the new python lessons. So having ago at the roll a dice activity and being asked how to allow a user to choose which dice to throw.
So using stack overflow I seemed to have figured this out, even though I don't fully understand the code.
I have used this stack overflow page to help me.
So far this is more of a copy and paste, with a few edits to get the code working and the menu shorter as there are only 2 options in the menu other than exit.
This may not be all that useful, I will try and break down the code below.
from noemoji import *
from datetime import *
from random import randint
imports the various modules needed
class switch(object):
value = None
def __new__(class_, value):
class_.value = value
return True
Allows user to choose options from a menu, by creating an object switch as part of a class
def case(*args):
return any((arg == switch.value for arg in args))
Something to do with user imput and passing this back to the switch class
def roll_dice():
print(python, 'can make a dice', dice)
roll = randint(1, 6) #Generate a random number between 1 and 6
print('You rolled a', roll) #Print the value of the roll variable
#print(fire * roll) #Repeat the fire emoji to match the dice roll
def roll_dice2():
print(python, 'can make randome numbers ', dice)
roll = randint(1, 12) #Generate a random number between 1 and 12
print('You rolled a', roll) #Print the value of the roll variable
#print(fire * roll) #emoji to match the dice roll
Functions to roll the dice, so create a random number between two values depending on the size of dice required.
def show_menu():
while True:
print ("What do you want to do?")
print ("1. Six sided dice")
print ("2. Twelve sided dice")
print ("3. Exit the program\n")
ask_for_input = input('Enter Option\n')
ask_for_input = int(ask_for_input)
while switch(ask_for_input):
if case(1):
roll_dice()
break
if case(2):
roll_dice2()
break
if case(3):
break
break
Create a menu system, which uses the case and switch functions we created earlier
def main():
show_menu()
Function to call the menu
if __name__ == '__main__':
main()
Calls the above function to call the menu function
If this is wrong, then remember I am NOT a coding expert. I am just trying by best and struggling.