Reputation: 11
I'm writing a program that takes a user file (a list of numbers, in this case) and calculate the mean, median, and mode of the list. However, I'm having a lot of trouble figuring out how to convert the number strings into integers. When I try, I get the error: "Invalid literal for int() in base 10" and then the first number of my list. No matter what I try I can't seem to convert the list even though this is the solution I keep seeing. Because of this, when I sort my list, it does not sort in numerical order. My mode function also seems to not be working very well, and even after a few days I can't figure out why. I would attach the file but there doesn't seem to be a way to do that, sorry. Hopefully this is enough information to see what might be causing the issues.
def CalculateMode(numbers):
dictionary = dict()
for num in numbers:
if num in dictionary:
dictionary[num] = dictionary[num] + 1
else:
dictionary[num] = 1
maximum = max(dictionary.values())
for key in dictionary:
if dictionary[key] == maximum:
print("The mode is " + key + ".")
def Main():
openFile = open("testfile.txt", 'r')
data = openFile.read()
numbers = data.split()
for num in numbers:
num = int(num)
return num
numbers = sorted(numbers)
print(numbers)
while True:
choice = input("Calculate Mode [1] Exit [2]: ")
if choice == "1":
CalculateMode(numbers)
elif choice == "2":
break
else:
print("Please choose one of the above options.")
Main()
Upvotes: 0
Views: 44
Reputation: 17156
One option using as much of your code as possible is the following.
Note: Renamed variables to following the Python convention Pep 8 which is:
Function names should be lowercase, with words separated by underscores as necessary to improve readability.
Variable names follow the same convention as function names.
Code
import re
def calculate_mode(numbers):
dictionary = dict()
for num in numbers:
if num in dictionary:
dictionary[num] = dictionary[num] + 1
else:
dictionary[num] = 1
maximum = max(dictionary.values())
for key in dictionary:
if dictionary[key] == maximum:
print(f"The mode is {key}.")
def main():
with open("testfile.txt", 'r') as open_file:
# Extract consecutive digits using regex
# (see https://www.geeksforgeeks.org/python-extract-numbers-from-string/)
data = re.findall(r"\d+", open_file.read())
# Convert digits to integer
numbers = [int(x) for x in data]
# Sort numbers (inplace)
numbers.sort()
print(numbers)
while True:
choice = input("Calculate Mode [1] Exit [2]: ")
if choice == "1":
calculate_mode(numbers)
elif choice == "2":
break
else:
print("Please choose one of the above options.")
main()
Input File (testfile.txt)
2, 3, 4, 5,
7, 9, 3, 2
1, 2, 9, 5
4, 2, 1, 8
6, 3, 4, 5
Output
[1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 7, 8, 9, 9]
Calculate Mode [1] Exit [2]: 1
The mode is 2.
Calculate Mode [1] Exit [2]:
Upvotes: 1
Reputation: 435
Try cast using int(float(desired_string))
The string you're trying to cast is not convertible to int.
Edit: As gold_cy said, there is a logical inconsistency in your code that goes beyond the error presented.
Upvotes: 0