#🔒 struggling to find the most bytes, least bytes , average bytes .

65 messages · Page 1 of 1 (latest)

polar magnet
#

I need to create a value returning function
That’s look through a firewall.txt file
I need to loop through the data and determine most,least, average bytes

orchid violetBOT
#

@polar magnet

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

heavy canopy
#

can you post your code as text

#

!code

orchid violetBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

tribal patio
#

By bytes, do you mean the largest number? Like in a file of 89, 12, 10 it would return 89 as highest byte?

polar magnet
#

Correct

#

The data file in question

#

When I run the most_bytes= int…..

tribal patio
#
def analyze_numbers_from_file(file_path):
     with open(file_path, 'r') as file:
         # Read all lines and split into a list of numbers
         numbers = [float(line.strip()) for line in file]

         if not numbers:
             print("The file is empty or contains no valid numbers.")
             return

         # Calculate required values
         largest_number = max(numbers)
         smallest_number = min(numbers)
         average = sum(numbers) / len(numbers)

         print(f"Largest number: {largest_number}")
         print(f"Smallest number: {smallest_number}")
         print(f"Average: {average}")
orchid violetBOT
#

Hey @tribal patio!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
polar magnet
#

It just returns 98 which is the first value but I need it to look through the whole list and list output the highest

tribal patio
#

That code would work for a text file thats like just filled with numbers, but your file is not like that

polar magnet
#

import csv

def main():
# Constants for menu choices
CHOICE_STATS = 1
CHOICE_FTP_ANALYZE = 2
CHOICE_ANALYZE_TELNET_SSH = 3
CHOICE_ANALYZE_DHCP_BOOTP = 4
COUNT_PACKETS_PER_PORT = 5
SAVE_STATS_TO_FILE = 6
QUIT = 99

# Reading the  firewall data from file
firewall_list = read_firewall_file()
firewall_stats = {}
while True:
    display_menu()
    choice = get_choice()
    if choice == 1:
        print("\nDescriptive Statistics for Bytes:")
        get_stats(firewall_list)
    elif choice == CHOICE_FTP_ANALYZE:
        analyze_ftp_traffic(firewall_list)
    elif choice == CHOICE_ANALYZE_TELNET_SSH:
        analyze_telnet_ssh_traffic(firewall_list)
    elif choice == CHOICE_ANALYZE_DHCP_BOOTP:
        analyze_dhcp_bootp_traffic(firewall_list)
    elif choice == COUNT_PACKETS_PER_PORT:
        count_packets_per_port(firewall_list)
    elif choice == SAVE_STATS_TO_FILE:
        save_stats_to_file(firewall_stats)
    elif choice == QUIT:
        print("Exiting program.")
        break

def read_firewall_file():
with open('FirewallFile-2.txt', 'r') as fw_file:
fw_reader = csv.reader(fw_file, delimiter='\t')
firewall_list = [row for row in fw_reader]
print("The number of rows in the firewall list is ", firewall_list)
return firewall_list
def display_menu():
print('\nPlease choose from the following menu:')
print('1. Calculate descriptive stats for bytes')
print('2. Analyze FTP traffic')
print('3. Analyze Telnet and SSH traffic')
print('4. Analyze DHCP and BOOTP traffic')
print('5. Count packets for each destination port')
print('6. Save descriptive stats to a file')
print('99. Quit')
def get_choice():
user_choice = int(input("Enter your choice: "))
if user_choice in [1, 2, 3, 4, 5, 6, 99]:
return user_choice
else:
print('Invalid choice. Enter a number from the menu')

#

my current code

#

shit i know its alot and needs cleaning up

#

def get_stats(firewall_list):
# trying to find the highest byte
most_bytes = int((firewall_list[1][3]))
print(most_bytes) this is the function im struggling with

#

when user enters 1

#
  • cries in python*
tribal patio
#

I dont quite understand firewall_list

#

like what does it contain?

#

Is it a list or a tuple or array

polar magnet
#

its a list that contains the data from the file

tribal patio
#

Cause up here

print("The number of rows in the firewall list is ", firewall_list)

#

Can u print the list for me and show me the result?

#

Like instead of printing max_byte

#

print firewall_list

polar magnet
#

yes

#

its 717 list worth of data

tribal patio
#

Ok, so you need to extract only the numbers into a list, do you have code for that?

polar magnet
#

i dont

#

i didnt think i need it to , but would that code go into def reading_file

tribal patio
#

Im not exactly sure, but u can do

numbers = []

for x in firewall:
    numbers.append(x[3])

largest_number = max(numbers)
smallest_number = min(numbers)
average = sum(numbers) / len(numbers)
#

So here, you seperate the massive list into sublists, like smaller lists containing something like ['0', '0', 'deny', '98', '1'], then take the 4th number and add it to a list called numbers and u get the results

#

But for the first sublist

#

Umm

polar magnet
#

thank you 😭

tribal patio
#

No

#

The problem is not fixed yet

polar magnet
#

fuck im confused with this project might take an L

#

its 12 pages worth of instrutions like

tribal patio
#

rn the code will add 'bytes' to the list

#

just do this instead

polar magnet
tribal patio
#

So this, just add numbers.remove("bytes")

#

What is this?

#

School project?

polar magnet
#

yeah school project

tribal patio
#

What grade?

polar magnet
#

college

#

ITP 270

#

programming for cybersecurity

tribal patio
#

Dayum!

polar magnet
#

stressing

tribal patio
#

no wonder its so confusing

polar magnet
#

😭

#

Thanks thought for your help

tribal patio
#

For the formatting, I also dont get it, and neither do I get the rest of ur project, but for the organizing part thats my code

tribal patio
#
numbers = []

for x in firewall:
    numbers.append(str(x[3]))

numbers.remove('bytes')

largest_number = max(numbers)
smallest_number = min(numbers)
average = sum(numbers) / len(numbers)
polar magnet
#

gonna try that

tribal patio
#

Thats it from me, maybe someone better, older with more understanding can help you. Good luck! Bye!

orchid violetBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.