Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Arrays
#1
Hello Gurus,

I'm trying to create a text chart that will show the statistics from this code.I'm working on Arrys but I'm not sure how to create the text chart with all of my scores.
print("Enter the home team's name")

home_team = input (">")

print("Enter the visitor team's name")

visitors_team = input (">")

print("How many points did the Mountaineers score in quarter 1?")

M_q1 = input (">")

print("How many points did the Fighting Irish score in quarter 1?")

F_q1 = input (">")

print("How many points did the Mountaineers score in quarter 2?")

M_q2 = input (">")

print("How many points did the Fighting Irish score in quarter 2?")

F_q2 = input (">")

print("How many points did the Mountaineers score in quarter 3?")

M_q3 = input (">")

print("How many points did the Fighting Irish score in quarter 3?")

F_q3 = input (">")

print("How many points did the Mountaineers score in quarter 4?")

M_q4 = input (">")

print("How many points did the Fighting Irish score in quarter 4?")

F_q4 = input (">")

print("Total Statistics")

print("Quarter", "Monuntaineers", "Fighting Irish")

I'm trying to create a text chart for these statistics:
Output:
Quarter Mountaineers Fighting Irish
1 >>>>> 7 >>>>> 7
2 >>>>> 10 >>>>> 7
3 >>>> 0 >>>>> 14
4 >>>>> 21 >>>>> 0
Total >>>> 38 >>>>> 28
Output:
I have tried the following but I'm not getting the correct results.
(1)
#quarters = [1, 2, 3, 4]

#pquarters = ""

#for values in quarters:
    
    #pquarters += str(quarters) 

    #print(pquarters)

    #print("Finished...")
Reply
#2
the code goes in between python tags
output goes between output tags
Reply
#3
Hello Gurus,

I'm trying to create a text chart that will show the statistics from this code.I'm working on Arrys but I'm not sure how to create the text chart with all of my scores.

print("Enter the home team's name")

home_team = input (">")

print("Enter the visitor team's name")

visitors_team = input (">")

print("How many points did the Mountaineers score in quarter 1?")

M_q1 = input (">")

print("How many points did the Fighting Irish score in quarter 1?")

F_q1 = input (">")

print("How many points did the Mountaineers score in quarter 2?")

M_q2 = input (">")

print("How many points did the Fighting Irish score in quarter 2?")

F_q2 = input (">")

print("How many points did the Mountaineers score in quarter 3?")

M_q3 = input (">")

print("How many points did the Fighting Irish score in quarter 3?")

F_q3 = input (">")

print("How many points did the Mountaineers score in quarter 4?")

M_q4 = input (">")

print("How many points did the Fighting Irish score in quarter 4?")

F_q4 = input (">")

print("Total Statistics")

print("Quarter", "Monuntaineers", "Fighting Irish")
These are my answers to the input above and I'm not sure how to create a text chart to represent these numbers.

Output:
[b]Quarter[/b] [b]Mountaineers[/b] [b]Fighting Irish[/b] 1 7 7 2 10 7 3 0 14 4 21 0 [b]Total[/b] 38 28
Thanks in advance for your help in this matter.

aka2d7 Cool
Reply
#4
Don't open another thread with same question. And follow instructions by Larz60+ about code tags.
Reply
#5
For starters I recommend using Python's builtin string formatting capabilities. Take a look at formatting examples here. Particularly the last one in section 6.1.3.2. ought to be very useful for you.

You can also have a look at Python tabulate library. It can make nice tabular data prints once you get the hang of it.

Edit:
By "arrays" you maybe mean lists - that's what they are called in Python at least. Official Python's tutorial on lists is a great place to start exploring their usage. Everything you need is here in examples.
Reply
#6
Hello Gurus,

I have tried this but I'm not making much progress can you please help.

Error:
Traceback (most recent call last): File "/Users/aka2d7/Documents/Step 7 Arrays.py", line 65, in <module> total_scores["scores1", "scores2", "scores3", "scores4"] NameError: name 'total_scores' is not defined
print("Quarter", "Monuntaineers", "Fighting Irish")

quarters = [1,2,3,4]

scores1 = [7,7]
scores2 = [10,7]
scores3 = [0,14]
scores4 = [21,0]

total_scores["scores1", "scores2", "scores3", "scores4"]
print("total_scores")
The output that I'm looking for is below:

Output:
Quarter Mountaineers Fighting Irish 1 7 7 2 10 7 3 0 14 4 21 0 Total 38 28
Can you please give me some help on how to achieve this outcome? Thanks in advance for all of the help and your support.

Regards,
aka2d7
Reply
#7
total_scores["scores1", "scores2", "scores3", "scores4"]
This isn't the right syntax.
You can define a list with total_scores=["scores1", "scores2", "scores3", "scores4"]
And then access elements with [indexing]:
>>>total_scores=["scores1", "scores2", "scores3", "scores4"]
>>>total_scores[0]
'scores1'
>>>total_scores[1]
'scores2'
To print it in the way you specified you best use string formatting:
my_list=["one", "two", "three"]
my_numbers1 = [1 ,2 ,3]
my_numbers2 = [4, 5, 6]

print("{:<5}{:^5}{:>5}".format(my_list[0], my_list[1], my_list[2]))
for a_list in [my_numbers1, my_numbers2]:
    print("{:<5}{:^5}{:>5}".format(a_list[0], a_list[1], a_list[2]))
That should give you all the information you need to conclude your task ;) And for details, refer to the sources I linked in previous post, only that's how you can really learn and understand.
Reply
#8
If think the way to format data,it will be a lot easier to use in many scenarios.
If keep data in dictionary then can load data straight into Pandas or tabulate.
import pandas as pd

data = {
    'Quarter': [1, 2, 3, 4],
    'Monuntaineers': [1, 10, 0, 21],
    'Fighting Irish': [7, 7, 14, 0]
}

df = pd.DataFrame(data, columns=['Quarter', 'Monuntaineers', 'Fighting Irish'])
Use:
>>> df
   Quarter  Monuntaineers  Fighting Irish
0        1              7               7
1        2             10               7
2        3              0              14
3        4             21               0  

>>> df['Monuntaineers'].sum()
38
The advantage when getting data into pandas is that is get easy to sum(),making graph and a lot more stuff.
And use of Jupyter Notebook it look better.

Tabulate can also load this data.
>>> from tabulate import tabulate
>>> print(tabulate(data, headers='keys', tablefmt='psql'))
+-----------+-----------------+------------------+
|   Quarter |   Monuntaineers |   Fighting Irish |
|-----------+-----------------+------------------|
|         1 |               7 |                7 |
|         2 |              10 |                7 |
|         3 |               0 |               14 |
|         4 |              21 |                0 |
+-----------+-----------------+------------------+
Reply
#9
Hi j.carter,

I ended up doing this for the code:
my_list=["Quarters"," " "Mountaineers"," " "Fighting Irish"]
my_scores1 = [1, 7, 7]
my_scores2 = [2, 10, 7]
my_scores3 = [3, 0, 14]
my_scores4 = [4, 21, 0]
 
print("{:<5}{:^10}{:>10}".format(my_list[0], my_list[1], my_list[2]))
for a_list in [my_scores1, my_scores2, my_scores3, my_scores4]:
    print("{:<5}{:^10}{:>10}".format(a_list[0], a_list[1], a_list[2]))
I appreciate your help in this request for understanding.

aka2d7
Reply
#10
aka2d7 Wrote:I appreciate your help with my question. I still don't understand how to code the last line of the output where it shows the total 38 28.
To answer your PM question here,as we do not do help on PM.
You have managed to make it difficult as a list has values from all header data.
A list should have content that that's relate to name,not spread over 4 list.
>>> Quarter = [1, 2, 3, 4]
>>> # Now it's easy to sum up
>>> sum(Quarter)
10
The next step to make it easier can be to build a dictionary.
>>> Quarter = [1, 2, 3, 4]
>>> Monuntaineers =  [7, 10, 0, 21]
>>> Fighting_Irish = [7, 7, 14, 0]
>>> 
>>> # Build a dicionary
>>> data = {}
>>> data['Quarter'] = Quarter
>>> data['Monuntaineers'] = Monuntaineers
>>> data['Fighting_Irish'] = Fighting_Irish
>>> data
{'Fighting_Irish': [7, 7, 14, 0],
 'Monuntaineers': [7, 10, 0, 21],
 'Quarter': [1, 2, 3, 4]}

>>> # Exmaple sum up Monuntaineers
>>> sum(data['Monuntaineers'])
38
Now is the data structured as show in my previous post.
Will now work in Pandas and Tabulate as shown.

If just need to sum up column,then can you mix something together with info as given.
If need to more with data then is Pandas really strong.
Here a run in Pandas where sum up all column or row,see that line and all calculation is added automatic.
G:\Anaconda3
λ python -m ptpython

>>> import pandas as pd
...
... data = {
...     'Quarter': [1, 2, 3, 4],
...     'Monuntaineers': [7, 10, 0, 21],
...     'Fighting Irish': [7, 7, 14, 0]
... }
...
... df = pd.DataFrame(data, columns=['Quarter', 'Monuntaineers', 'Fighting Irish'])

>>> # To sum up all column with a total line
>>> df.loc['Column sum']= df.sum()
>>> df
            Quarter  Monuntaineers  Fighting Irish
0                 1              7               7
1                 2             10               7
2                 3              0              14
3                 4             21               0
Column sum       10             38              28


>>> # To sum up all row with a total line
>>> df['Row sum'] = df.iloc[:, 1:].sum(axis=1)
>>> df
   Quarter  Monuntaineers  Fighting Irish  Row sum
0        1              7               7       14
1        2             10               7       17
2        3              0              14       14
3        4             21               0       21

# singel sum
>>> df.Monuntaineers.sum()
38
A plot of data done in Jupyter Notebook:
[Image: bw5zZa.jpg]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  comparing floating point arrays to arrays of integers in Numpy amjass12 0 1,625 Jul-26-2021, 11:58 AM
Last Post: amjass12
  Numpy arrays and compatability with Fortran arrays merrittr 0 1,872 Sep-03-2019, 03:54 AM
Last Post: merrittr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020