Recently I've started learning Python and I'm trying to create a basic game to practice several topics. Basically, I'm trying to create a baseball batting simulator based on batting average (accuracy) and the import random
module.
I want the user to be able to choose a batter when the game loads up. This batter would be an object with two attributes: name and accuracy.
For example:
class Batter:
def __init__(self,name,accuracy):
self.name = name
self.accuracy = accuracy
p1 = Batter("Bob",303)
p2 = Batter("Mike",373)
I know I can input this all manually, but say I have a csv file called 'data.csv' with hundreds of batter names and accuracy values. In the form of:
Batter, At-Bats, Batting Average
Bob, 300, 303
Mike 274, 373
Nick, 313, 234
Joe,233, 280.....
The idea would be to pull the first and third columns of the csv. It has been driving me nuts because I can't figure out a way to automate this process and be able to pull data from the csv into individual object classes.
My goal was to have everything loaded in from the csv, and when the game would start out it would ask the user: "Please choose a player:". If the user inputs "Joe" it would pull his attributes and set the accuracy to his value of 280 and use the following command to determine whether he hits the ball or not. Imagine Joe was loaded in with the variable "p4"
import random
acc = p4.accuracy
n = p4.name
number = random.randint(1,1000)
if number <= acc:
print("%s hit the ball!" % n)
else:
print("%s did not hit the ball!" % n)
Just struggling with the automation process of object creation from csv data is all. Any help would be appreciated!