0

I need some help regarding calculating averages and ranges. I am using built-in functions such as sum(), len(), etc. and cannot seem to calculate the average or range. I am using it to write a small piece of code for fun but cannot seem to get it to work. any help is much appreciated. Thank you!

x = 1
number_list = []

while x == 1:
     input_number = input("PLease input an integer")
     if str.isdigit(input_number) == True:
         number_list.append(input_number)
     else:
         print("Please input a valid integer only.")
     continueornot = input("Would you like to continue adding data? PLease input 'Yes' to continue, and anything else to quit.")
     if continueornot == 'Yes':
        x = 1
     else:
           
            print("Here is the maximum number:", max(number_list))
            print("Here is the minimum number:", min(number_list))
            print("Here is the count:", len(number_list))
            print("Here is the average:" + sum(number_list) / len(number_list))
            print("Here is the range:", range(number_list))
            quit()
Anonymous
  • 19
  • 4
  • What is not working? What input did you provide and what (wrong) results did you get? – jarmod Jul 20 '21 at 23:43
  • I use numbers as the input as I have code to filter out non-integer answers, and when it reaches the average calculation it reads "TypeError: unsupported operand type(s) for +: 'int' and 'str' " – Anonymous Jul 20 '21 at 23:46
  • That's because you need to cast `input_number` to an int when you put it in your list. – Chrispresso Jul 20 '21 at 23:48
  • 1
    When you take input, it is automatically a string. https://stackoverflow.com/questions/43415978/why-does-input-always-return-a-string You can enclose it in int() like this: `number = int(input('give number'))` You should also be aware of your if/else chaining and the loop. I suggest you go through step by step and consider when it loops back. – ᴓᴓᴓ Jul 20 '21 at 23:48
  • Thank you! Ill go through the code again and use your advice. – Anonymous Jul 20 '21 at 23:50
  • Besides the syntax errors, the logic an be improved and simplified further here. – Daniel Hao Jul 21 '21 at 00:34

2 Answers2

0

Change

if str.isdigit(input_number) == True:
    number_list.append(input_number)

to

if input_number.isdigit():
    number_list.append(int(input_number))

The error is because you're trying to do those operations on a list of strings.

You can also remove the check against True since that is implicitly checking the truthiness and since input_number is already a str, you can call the isdigit() method directly.

Chrispresso
  • 3,660
  • 2
  • 19
  • 31
  • You don't have to do extra step here. since the input_number is a string already, you can just check - if `input_number.isdigit()` that's it. Then do next . – Daniel Hao Jul 21 '21 at 00:11
  • I'd also remove the ` == True` from the boolean comparison. – jarmod Jul 21 '21 at 13:32
0

The problem is that you are appending strings to the list rather than integers and then you are applying arithmetic operations on it. So first you should convert the input number to int type. Secondly range function will not give you the range of a list rather then it returns a sequence.

x = 1
number_list = []

while x == 1:
    input_number = input("PLease input an integer")
    if str.isdigit(input_number) == True:
        input_number=int(input_number)
        number_list.append(input_number)
    else:
        print("Please input a valid integer only.")
    continueornot = input("Would you like to continue adding data? PLease input 'Yes' to continue, and anything else to quit.")
    if continueornot == 'Yes':
        x = 1
    else:

        print("Here is the maximum number:", max(number_list))
        print("Here is the minimum number:", min(number_list))
        print("Here is the count:", len(number_list))
        print("Here is the average:" , sum(number_list) / len(number_list))
        print("Here is the range:", max(number_list)-min(number_list))
 
Dharman
  • 30,962
  • 25
  • 85
  • 135
Newbie
  • 3
  • 1