Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Brock Lammers
4,991 PointsSetting an occurrence percentage for a random.choice
I am writing a little baseball game to test while loops and the use of the random function.
Can I set an occurrence percentage for the variables in "pitches" i.e. 'fastball' is thrown 50% of the time and 'curve' is thrown 30% of the time and so on? I still want it to select them from random but want it to take the percentage in to account.
I have been coding for all but 2 weeks so go easy on my code please. Thanks.
import random import os import sys
pitches = ['fastball', 'curve', 'changeup'] call = ['ball', 'strike'] balls = 0 strikes = 0 pitch_count = 0
def next_batter(): nextBatter = input("Would you like to play another batter? Y/n ").lower() if nextBatter != 'n': pitchAgain() else: sys.exit()
def welcome(): start = input("would you like to send your batter to the plate? Y/n") if start == 'n': print("BYE!") sys.exit() else: return True
def pitchAgain(): balls = 2 strikes = 1 count = 1
while True:
thrown = random.choice(pitches)
print ("Pitch number {} was a {}".format(count,thrown))
count +=1
umpire = random.choice(call)
if umpire == "ball":
print("umpire called it a ball")
print("The count is {} balls and {} strikes".format(balls,strikes))
balls += 1
else:
print("umpire called it a Strike")
strikes += 1
print("The count is {} balls and {} strikes".format(balls,strikes))
if count == 5:
print("you need a new pitcher")
sys.exit()
if balls == 3 and strikes == 2:
print("Full Count")
elif balls >= 4:
print("walked the batter")
next_batter()
elif strikes >= 3:
print("Stuck him out!")
next_batter()
while True: welcome() pitchAgain()
1 Answer
Jason Anello
Courses Plus Student 94,610 PointsHi Brock,
Here's a simple, but probably crude, solution you can use when you have a small list of choices.
You can duplicate the choices so that they are in the correct ratio you want.
Example:
pitches = ["fastball"] * 5 + ["curve"] * 3 + ["changeup"] * 2
that creates the list
['fastball', 'fastball', 'fastball', 'fastball', 'fastball', 'curve', 'curve', 'curve', 'changeup', 'changeup']
fastball should come up about 50% of the time since it occupies half of the choices. about 30% for curve and 20% for changeup.
This would probably be too inefficient when you have a large number of choices, the list would be too big after duplicates, but should be ok for a small number of choices like you have here.