Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

David Houghton
PLUS
David Houghton
Courses Plus Student 1,151 Points

Help with coin flip/die roll game

I am writing a program that asks the user to choose to flip a coin or roll a die. Once they select coin or die, it asks them how many times they want to flip/roll. I then want to be able to return the results into lists where I am able to show the average and mode for the die roll, and show which coin side appeared more often for the coin flip. Here is my program so far:

import random

print(""" Use these games of chance to make your next major life choice. Type '2' to flip a coin, '3' to play Rock-Paper-Scissors, '6' to roll a die: """)

game_of_chance = 2 or 6

while game_of_chance != 2 or 6: game_of_chance = int(input("Please enter 2 or 6: ")) if game_of_chance == 2: print("You chose to flip a coin") break elif game_of_chance == 6: print("You chose to roll a die") break

num_tries = () coin_sides = ['Heads', 'Tails'] die_sides = [1, 2, 3, 4, 5, 6] game_choice = ()

def coin_dice(num_tries): while num_tries > 0: tries = random.choice(game_choice) num_tries -= 1 print(tries)

if game_of_chance == 2: num_tries = int(input("How many times would you like to flip the coin?: ")) game_choice = coin_sides coin_dice(num_tries) elif game_of_chance == 6: num_tries = int(input("How many times would you like to roll the die?: ")) game_choice = die_sides coin_dice(num_tries)

My questions are these: 1) How can I return every result from my function into a list? So far, my attempts to list the results ends up with me listing only the final result (probably because it is a while loop). How can I collect all results into a list that I can use for other functions? 2) What can I change in my first while loop so that it doesn't show the user both the print statement at the top and the "Please enter 2 or 6: " string? So far, in all situations, it shows both, rather than showing the second only when the user doesn't enter 2 or 6.

Feel free to ask me for further clarification if necessary. Thanks for the help!

2 Answers

Steven Parker
Steven Parker
215,987 Points

When posting code, be sure to use Markdown formatting. See the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

For accumulating the results in a loop, you can use the list "append" method:

  tries = []                                  # initialize a new list
  while num_tries > 0:
    tries.append(random.choice(game_choice))  # add the result to the list

Then, to avoid the "2 or 6" question the first time, you must convert the first message into an input to give the user an opportunity to provide the answer there. The "while" will then run only if the answer doesn't fit the criteria.

Also, when combining tests with logic, you must have complete comparisons on both sides of the logic:

# convert the first "print" to an "input":
game_of_chance = int(input("""Use these games of chance to make your next major life choice.
Type '2' to flip a coin, or '6' to roll a die: """))

# then test using COMPLETE expressions:
while game_of_chance != 2 and game_of_chance != 6:
David Houghton
PLUS
David Houghton
Courses Plus Student 1,151 Points

Thank you so much! This fixed both of my problems. And I will make sure to use Markdown formatting in the future. Thanks again!