1

I am learning Python using ZyBooks. This is LAB 3.14, "Detecting Network Change". The LAB provides a csv file to use to supply user credentials to be used in the evaluation.

"This program is going to manage user login times and attempt to detect any change in a users typical login attempts. It will use an input file to store data and read the file using the csv.reader( ) method. The file will contain a list of login_names, followed by login_time separated by commas.

Write a program that first reads in the name of an input file, reads the information stored in that file and determines if the user login has occurred at off hour times. The company employees work from 9 am to 5 pm so any other time would be an off hour login attempt. If the login attempt is made after hours, store the user name and login time in a dictionary with the user name as the key. Display all anomaly attempts at the end of the program. If there are no questionable login attempts display No anomaly login attempts"

Ex: If the input is:

input1.csv

and the contents of input1.csv are: (store time as integers and use military time to simulate am and pm attempts)

bob,2,paula,1,nancy,8,thomas,23,zach,22,charlotte,4

the output is:

Anomaly login attempts: nancy:8 thomas:23 zach:22 charlotte:4

The LAB starts as a clean slate, and the only thin provided is the input1.csv file to be downloaded.

The code I have thus far produces the correct response for the first row in the CSV only. It does not appear to loop over the contents as I expected.

This is the code I have so far:

import csv

anomalies = dict()
filename = input()
with open(filename, mode='r') as f:
    csv_reader = csv.reader(f)
    for row in csv_reader:
        name, time = row[0], row[1]
        time = int(time)
        # If time is between 9 am to 5pm(17) then it's normal
        if not (9 <= time <= 17):
            # If it belongs to off hours add to anomalies
            anomalies[name] = time
            if len(anomalies) == 0:
                # It means anomalies dictionary is empty, no anomalies
                print('No anomaly login attempts\n')
            else:
                print('Anomaly Login Attempts:')
                for key, value in anomalies.items():
                    print(key, value)


My output:

Anomaly Login Attempts: bob 2

Expected output:

Anomaly Login Attempts: bob 2 paula 1 nancy 8 thomas 23 zach 22 charlotte 4

Any help would be appreciated!

rb5064
  • 11
  • 3
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Dec 08 '22 at 00:30

1 Answers1

1

You wrote:

    for row ...
            anomalies[name] = time
            if len(anomalies) == 0:

It appears that Author's Intent was for the if test to happen after the for loop had finished reading the CSV file.

Indentation matters a great deal in python. Change the indentation so if and following statements are at the same level as the for. Then they will run once the for-loop completes.

In particular, the loop is trying to populate anomalies, and that's not complete until the for-loop finishes. Once it has, you can display that dict's results.

J_H
  • 17,926
  • 4
  • 24
  • 44
  • I tried editing the if len(anomalies) == 0: and the following statements by changing the indent to match the for statement as suggested. I still got the same results. The first two lines of output are correct, but the loop fails to iterate through the rest of the csv file, or it isn't printing the results... – rb5064 Dec 08 '22 at 01:27