0

I don't understand why the code is causing errors. For example, the error says that you can't add an integer and a string together, but I've already converted the string to an Integer. Could you help me fix it? The code is attached. Thanks.

# In this program I will collect data
# values from the user and use python built-in functions
# to display various info about the data set

# Giving user directions

print("In this programme, you can enter")
print("some numbers and it will display the")
print("minimum, maximum, range and average")
print("of the data set you entered.")

# Setup

list = []
loop = True

# Creating function for process of entering number

def enterNumber():
    print()
    x = input("Enter an integer: ")
    y = str.isdigit(x)
    if y == True:
        list.append(x)
        print()
        print("Successfully added to list")
        print()
        print("Here is your list so far")
        print(list)
    elif y == False:
        print()
        print("Sorry, this is not an integer")
    else:
        print("Error. Kill and start again")


while loop == True:
    enterNumber()
    print()
    print("Would you like to add another value?")
    print()
    a = input("Enter 1 for Yes, enter 0 for No: ")
    if a == "0":
        loop = False

print()
print()
print("------------------------------------------------")
print()
print("Count:", len(list))
print()
print("Minimum:", min(list))
print()
print("Maximum:", max(list))
print()
print("Range:", int(max(list)) - int(min(list)))
print()
print("Average:", int(sum(list)) / int(len(list)))

It seems like this last line is the problem.

Cardstdani
  • 4,999
  • 3
  • 12
  • 31
Kyle Shih
  • 11
  • 3
  • 1
    Please extract a [mcve]. This makes it easier for everybody (including yourself) to spot the mistake. – Ulrich Eckhardt Jul 02 '22 at 07:38
  • 2
    It's considered bad form to use `list` as a variable name since it's the name of a popular built-in function. – DarrylG Jul 02 '22 at 07:52
  • 1
    Note that as your code currently stands if you input `11` and `2` the `max` value will be `2` because as strings `'2' > '11'` – Nick Jul 02 '22 at 08:00

3 Answers3

4

You need to use int() function after checking if y value is True. If not you will be appending an string value always to your list:

def enterNumber():
    print()
    x = input("Enter an integer: ")
    y = str.isdigit(x)
    if y == True:
        list.append(int(x)) #int(x) converts x to integer
...
Cardstdani
  • 4,999
  • 3
  • 12
  • 31
1

The reason for the issue is that you have string values in your list, which doesn't work. Simple test:

l = ['1', '2']
sum(l)

results in the same error.

Easiest fix is mentioned by Cardstdani.

You have not asked for it. But there is a serious issue in your code: You should avoid at all cost to name your list list as this overwrites the built-in list() function.

MaxS
  • 71
  • 3
-1
# In this program I will collect data
# values from the user and use python built-in functions
# to display various info about the data set

# Giving user directions

print("In this programme, you can enter")
print("some numbers and it will display the")
print("minimum, maximum, range and average")
print("of the data set you entered.")

# Setup

list1 = []
loop = True

# Creating function for process of entering number

def enterNumber():
    print()
    x = input("Enter an integer: ")
    y = str.isdigit(x)
    if y == True:
        list1.append(x)
        print()
        print("Successfully added to list")
        print()
        print("Here is your list so far")
        print(list)
    elif y == False:
        print()
        print("Sorry, this is not an integer")
    else:
        print("Error. Kill and start again")


while loop == True:
    enterNumber()
    print()
    print("Would you like to add another value?")
    print()
    a = input("Enter 1 for Yes, enter 0 for No: ")
    if a == "0":
        loop = False
print()
print()
print("------------------------------------------------")
print()
print("Count:", len(list1))
print()
print("Minimum:", min(list1))
print()
print("Maximum:", max(list1))
print()
print("Range:", int(max(list1)) - int(min(list1)))
print()
list1 = list(map(int, list1))
print("Average:", sum(list1) / len(list1))
Mehmaam
  • 573
  • 7
  • 22