Home > Back-end >  Struggling to bring down functions
Struggling to bring down functions

Time:12-07

I have the code for my games but I need to let the user choose which game and i have a while loop for it it but im struggling to figure out how to call down the functions and print them. For im asking what the user would like to do.

import random
def coin_flip():
    gen_num = random.randint(0, 1)

    if gen_num == 0:
       return "Coin flip: Tails"
    else:
       return "Coin flip: Heads"

def roll_d6():
    gen_num = random.randint(1, 6)
    return f"D6 roll: {gen_num}"

def roll_d20():
    gen_num = random.randint(1, 20)
    return f"D20 roll: {gen_num}"

def pick_card():
    suit = random.randint(0, 3)
    if suit == 0: suit = "Spades"
    if suit == 1: suit = "Hearts"
    if suit == 2: suit = "Diamonds"
    if suit == 3: suit = "Clubs"
    value = random.randint(1, 13)
    if value == 1: value = "Ace"
    if value == 11: value = "Jack"
    if value == 12: value = "Queen"
    if value == 13: value = "King"

    return f"Your card: {value} of {suit}"

print('Welcome to the game arena!\nHere are your options:')
stop = False
while not stop:
    print('\t1) Flip a coin\n\t2) Pick a random playing card')
    print('\t3) Roll a 6-sided dice\n\t4) Roll a 20-sided dice')
    choice = input('What would you like to do? ')
    first = input()


else:
        print('Invalid Operation')
user_quit=input(' Do you want to quit? (y/n):')
if user_quit == 'y':
        stop = True

CodePudding user response:

You could try something like that:

print('Welcome to the game arena!\nHere are your options:')
stop = False
while not stop:
    print('\t1) Flip a coin\n\t2) Pick a random playing card')
    print('\t3) Roll a 6-sided dice\n\t4) Roll a 20-sided dice\n\tPress 0 to quit')
    choice = input('What would you like to do? ')

    try:
        num_chosen = int(choice)
    except ValueError:
        print("You chose invalid value, please try again.")
    else:
        if num_chosen == 1:
            coin_flip()
        elif num_chosen == 2:
            pick_card()
        elif num_chosen == 3:
            roll_d6()
        elif num_chosen == 4:
            roll_d20()

        else:
            user_quit=input('Do you want to quit? (y/n):')
            if user_quit.lower() == 'y':
                    stop = True

CodePudding user response:

What about a "switch" like solution. All your choices are in the dict. switch.get(choice, show_error) does return the function stored in the dict or the show_error function if the choice is not found in the dict. The second attribute of get() is the default option if nothing was found.

The content of the dict is assigned to action and then called with action(). Note the missing () for the functions inside the dict. This way the function is given to the action variable instead of the result of the function.

import random
def coin_flip():
    gen_num = random.randint(0, 1)

    if gen_num == 0:
       return "Coin flip: Tails"
    else:
       return "Coin flip: Heads"

def roll_d6():
    gen_num = random.randint(1, 6)
    return f"D6 roll: {gen_num}"

def roll_d20():
    gen_num = random.randint(1, 20)
    return f"D20 roll: {gen_num}"

def pick_card():
    suit = random.randint(0, 3)
    if suit == 0: suit = "Spades"
    if suit == 1: suit = "Hearts"
    if suit == 2: suit = "Diamonds"
    if suit == 3: suit = "Clubs"
    value = random.randint(1, 13)
    if value == 1: value = "Ace"
    if value == 11: value = "Jack"
    if value == 12: value = "Queen"
    if value == 13: value = "King"

    return f"Your card: {value} of {suit}"

def show_error():
    return 'Invalid input, please select from the following:\n'


def quit_game():
    global stop
    stop = True
    return 'Thanks for playing'


print('Welcome to the game arena!\nHere are your options:')
stop = False
while not stop:
    print('\t1) Flip a coin\n\t2) Pick a random playing card')
    print('\t3) Roll a 6-sided dice\n\t4) Roll a 20-sided dice')
    print('\tq) Quit')
    choice = input('What would you like to do? ')
    switch = {
        '1': coin_flip,
        '2': pick_card,
        '3': roll_d6,
        '4': roll_d20,
        'q': quit_game
    }
    action = switch.get(choice, show_error)
    print('=' * 10)
    print(action())
    print('=' * 10)
  • Related