'Calculate and print daily total and average for each server array

python beginner and needing help with assignment.

Any pointers and code would be appreciated. Problem is as follows: Your organization has three network servers that report daily connection statistics on access time. The data was captured 5 times daily for each day for each server. For each server, you will need to input today's data into 3 different parallel arrays (one for each server) with the access times for each. You will then need to total and average these values for each server.

  • Allow user to enter 5 entries in each of three servers' arrays.
  • Once the user has entered the daily values, use a loop to read values to total and average access time for each server
  • Calculate and print daily total and average for each server array.


Solution 1:[1]

There are several ways to take in user input, of which I am assuming your teacher wants you to read input from a command line.

user_input = input("Enter a value: ") will prompt the user to enter a value into the command line.

You can use a dictionary to store values for each of the servers - something like:

servers = {"server1": [], "server2": [], "server3": []} will allow you to have a single variable to hold data for each server.

You can access said data like so:

servers["server1"] will return your list - in this case its empty, so []

To add data to a list you can use [].append("data to add"), so to add data to that dictionary, it'd look like servers["server1"].append("data to add").

If you want to treat "server1", "server2", "server3" as variables, you can just iterate over them in a loop... that would look like this:

for server in servers:
    servers[server].append("data to append")
    # this will output the data in the array for each "server#"
    print(servers[server])

If you want a traditional for loop - too bad :P! :

for i in range(5):
    print(i)

You can access items of an array by using its index, provided that there is an item with that index:

arr = ["potato", "banana"]
print(arr[0])
# will print "potato"
print(arr[1])
# will print "banana"
print(arr[2])
# will throw an error, because it doesn't exist in arr

With this information, it should be enough to piece it all together.

servers variable
for each server in servers:
    for number of times to prompt user:
        servers[each server].append(input(prompt the user for data to append))

use the same loops to sum then average for each array

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shmack