HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

Printing a score sheet for baseball batters

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
batterssheetbaseballscoreprintingfor

Problem

I've been learning Python 2.7 for about a week now and have written a quick program to ask for names of batters and printing a score sheet. I have then added the names to a list which I will use more in other future functions.

I have a few questions:

-
I feel like there is too much repetition in my code, such as the prompting for each batter and all the printing. I am trying to figure out how I could use a loop to do these for me. Since I know I need 11 batters a for loop would be best but I need to make a new variable each time, how can I do this?

-
I have been reading around about the use of global. I can't seem to get a definite answer but is this the correct use if I want to use a variable from one function in multiple others?

```
def Input_Players():

global First, Second, Third, Fourth, Fith, Sixth, Seventh, Eigth, Ninth, Tenth, Eleventh

First = [raw_input("Name of 1st Batsman > ")]
Second = [raw_input("Name of 2nd Batsman > ")]
Third = [raw_input("Name of 3rd Batsman > ")]
Fourth = [raw_input("Name of 4th Batsman > ")]
Fith = [raw_input("Name of 5th Batsman > ")]
Sixth = [raw_input("Name of 6th Batsman > ")]
Seventh = [raw_input("Name of 7th Batsman > ")]
Eigth = [raw_input("Name of 8th Batsman > ")]
Ninth = [raw_input("Name of 9th Batsman > ")]
Tenth = [raw_input("Name of 10th Batsman > ")]
Eleventh = [raw_input("Name of 11th Batsman > ")]

def Print_Score_Sheet():
total_chars = 56

Batsman = First + Second + Third + Fourth + Fith + Sixth + Seventh + Eigth + Ninth + Tenth + Eleventh

print "1." + Batsman[0] + "." * (total_chars - len(Batsman[0]) + 2)
print "2." + Batsman[1] + "." * (total_chars - len(Batsman[1]) + 2)
print "3." + Batsman[2] + "." * (total_chars - len(Batsman[2]) + 2)
print "4." + Batsman[3] + "." * (total_chars - len(Batsman[3]) + 2)
print "5." + Batsman[4] + "." * (total_chars - len(Batsman[4]) + 2)
print "6." + Batsman[5] + "." * (total_char

Solution

I would structure the code like this:

def input_players(total=11):
  players = []

  for index in range(total):
    player = raw_input('Name of Batsman {} > '.format(index + 1))
    players.append(player)

  return players

def print_score_sheet(batsmen, total_chars=56):
  for index, batsman in enumerate(batsmen, 1):
    print '{}. {} '.format(index, batsman).ljust(total_chars, fillchar='.')

if __name__ == '__main__':
  players = input_players(11)
  print_score_sheet(players)


Some tips:

  • If you use a set of variables like Player1, Player2, PlayerN, you should be using a list to store them.



  • Your functions should do what they say. input_players() should return a list of players, nothing more, nothing less.



  • Get into the habit of including the if __name__ == '__main__': block. The stuff inside of the block is executed only if you run the Python script directly, which is good if you have multiple modules.

Code Snippets

def input_players(total=11):
  players = []

  for index in range(total):
    player = raw_input('Name of Batsman {} > '.format(index + 1))
    players.append(player)

  return players

def print_score_sheet(batsmen, total_chars=56):
  for index, batsman in enumerate(batsmen, 1):
    print '{}. {} '.format(index, batsman).ljust(total_chars, fillchar='.')

if __name__ == '__main__':
  players = input_players(11)
  print_score_sheet(players)

Context

StackExchange Code Review Q#12889, answer score: 13

Revisions (0)

No revisions yet.