#🔒 struggling to find the most bytes, least bytes , average bytes .
65 messages · Page 1 of 1 (latest)
@polar magnet
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.
By bytes, do you mean the largest number? Like in a file of 89, 12, 10 it would return 89 as highest byte?
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}")
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.
It just returns 98 which is the first value but I need it to look through the whole list and list output the highest
That code would work for a text file thats like just filled with numbers, but your file is not like that
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*
I dont quite understand firewall_list
like what does it contain?
Is it a list or a tuple or array
its a list that contains the data from the file
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
Ok, so you need to extract only the numbers into a list, do you have code for that?
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
thank you ðŸ˜
fuck im confused with this project might take an L
its 12 pages worth of instrutions like
yeah school project
What grade?
Dayum!
stressing
no wonder its so confusing
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
I really didn't help, gl!
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)
gonna try that
Thats it from me, maybe someone better, older with more understanding can help you. Good luck! Bye!
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.