Formatting Without A Table

This is the place for queries that don't fit in any of the other categories.

Formatting Without A Table

Postby jeshie » Wed Sep 11, 2013 1:08 am

I had to make a program where I asked for user input about a Quidditch match and after calculating certain values print out the statistics of the game in an organized manner similar to a table, but was told I wasn't allowed to actually use a table to format it since we haven't reached that point in the book, which I thought was kind of ridiculous.

It is supposed to look like the image in the attached file Donkey and It is easily possible to make it organized with the names Griffindor and Hufflepuff because they have the same number of characters so it just takes trial and error for proper spacing. The problem is I can't get it to look organized if the team names are of a different length, which is what I am curious about.

As for the assignment, I've already been told its good enough and have turned it in since its due at the end of the period, but I really wanted to know if there was a more efficient way that could work with varying string lengths.

Code: Select all
print("\nHouse\t\tGoals\t\tSnitch\t\tGoals per Minute")
print(" -----\t\t-----\t\t------\t\t----------------")
print(team1, "\t", format(score1, "4d"), "\t\t     1\t\t", format(gpm1, "15.2f"))
print(team2, "\t", format(score2, "4d"), "\t\t     0\t\t", format(gpm2, "15.2f"))
print("\nTotal goals for the game:", end="   ")
print(total)
print("Average time between goals:", end="   ")
print(format(agpm, ".2f"))


If you want to see all of the source code it is in the quidditch_statistcs.txt file and isn't very long. The part referencing about is the last bit after the last comment. I'm also using Python ver 3.3.2 in case anyone needed to know that.
Attachments
quidditch_statistics.txt
Source Code
(1.58 KiB) Downloaded 27 times
donkey.jpg
Image of What it is supposed to look like after running it
donkey.jpg (85.1 KiB) Viewed 242 times
Last edited by micseydel on Wed Sep 11, 2013 1:12 am, edited 1 time in total.
Reason: First post lock.
jeshie
 
Posts: 2
Joined: Wed Sep 11, 2013 12:18 am

Re: Formatting Without A Table

Postby kirbyfan64sos » Thu Sep 12, 2013 6:24 pm

What you need is ljust and rjust. They add extra padding spaces depending on the string length. Say I have the string 'Hello world!'. I want to make the string 15 characters long. I just use `'Hello world!'.ljust(15)` to get 'Hello, world! '. The padding is useful for formatting tables.

Here is a modified code:

Code: Select all
#Initializes the variables to proper data type.

team1 = ""
team2 = ""
score1 = 0
score2 = 0
time = 0
total = 0
agpm = 0.0
gpm1 = 0.0
gpm2 = 0.0
x = 0
y = 0

#Displays introduction information for program.

print("\tWelcome to CSE 1284 Quidditch Statistics Calculator")
print("\nEnter the teams, scores, and time to make it work.  It\'s like magic!")

#Prompts user for team names, scores, and the time and displays them.

team1 = input("\nEnter the name of the team that caught the golden snitch: ")
print("What was ", team1, "'s final score", end="")
score1 = int(input("? "))
team2 = input("Enter the name of the other team: ")
print("What was ", team2, "'s final score", end="")
score2 = int(input("? "))
time = int(input("Enter the length of the game in minutes: "))

#Calculates the goals, goals per minute, total and overall average goals per
#minute.

score1 = (score1 - 150) / 10
score2 = score2 / 10
gpm1 = float(time) / float(score1)
gpm2 = float(time) / float(score2)
total = score1 + score2
agpm = float(time) / float(total)
score1 = int(score1)
score2 = int(score2)

#Displays a table with the appropriate placement of the statistics.

print("\nHouse\t\tGoals\t\tSnitch\t\tGoals per Minute")
print(" -----\t\t-----\t\t------\t\t----------------")
print(team1.ljust(13), format(score1, "4d"), "\t\t     1\t\t", format(gpm1, "15.2f"))
print(team2.ljust(13), format(score2, "4d"), "\t\t     0\t\t", format(gpm2, "15.2f"))
print("\nTotal goals for the game:", end="   ")
print(total)
print("Average time between goals:", end="   ")
print(format(agpm, ".2f"))



Sample run:

Code: Select all
        Welcome to CSE 1284 Quidditch Statistics Calculator

Enter the teams, scores, and time to make it work.  It's like magic!

Enter the name of the team that caught the golden snitch: hello
What was  hello 's final score? 12
Enter the name of the other team: worldx
What was  worldx 's final score? 13
Enter the length of the game in minutes: 60

House           Goals           Snitch          Goals per Minute
 -----          -----           ------          ----------------
hello          -13                   1                     -4.35
worldx           1                   0                     46.15

Total goals for the game:   -12.5
Average time between goals:   -4.80
This program is bug-free. If you see a bug, it's actually a feature.
kirbyfan64sos
 
Posts: 7
Joined: Sun Aug 25, 2013 10:45 pm

Re: Formatting Without A Table

Postby jeshie » Fri Sep 13, 2013 6:13 am

Just wanted to say thanks for helping. Wish my TA was anything close to this helpful.
jeshie
 
Posts: 2
Joined: Wed Sep 11, 2013 12:18 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests