Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Games such a flip a coin, rolling dice, and choosing cards

im trying to make a game board and i have taken some notes from my class and im just struggling to do this assignment. We never have done anything like this in class before. I need to do a coin flip, roll two dice a 6 sided one and a 20 sided one and i need to pick a card.

import random

def flip_coin():
    # TODO: implement this function
    # get a random int between 0 and 1
    # if the number is 0, return "Coin flip: Tails"
    # if the number is 1, return "Coin flip: Heads"
    # delete this next line:
    return 0

def roll_d6():
    # TODO: implement this function
    # get a random int between 1 and 6
    # return "D6 roll: [that number]"

    # delete this next line:
    return 0

def roll_d20():
    # TODO: implement this function
    # get a random int between 1 and 20
    # return "D20 roll: [that number]"

    # delete this next line:
    return 0

def pick_card():
    # TODO: implement this function
    # get a random number between 0 and 3
    # [card suit]: 0=spades, 1=hearts, 2=diamonds, 3=clubs
    # then get a random number between 1 and 13
    # [card value]: 1=Ace, 11=Jack, 12=Queen, 13=King, 2-10 are normal (no translation)
    # return "Your card: [card value] of [card suit]"

    # delete this next line:
    return 0


# TODO: put the following lines into a loop, to allow the user to repeat until they quit
print('Welcome to the game center!\nHere are your options:')
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? ')
# TODO: based on what the user selects, call the appropriate function above (print the result)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There are faster solutions to this: but these are the most simple solutions.

import random

def coin_flip():
    generated_num = random.randint(0, 1) # generate a random number from 0 to 1

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

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

def roll_d20():
    generated_num = random.randint(1, 20)
    return f"D20 roll: {generated_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}"

NOTE

Here is a better way to pick the suit:

def pick_card():
    suit = random.choice(['Spades','Hearts','Diamonds','Clubs'])
    ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading