[Tutor] HELP PLEASE

David L Neil PyTutor at DancesWithMice.info
Tue Aug 13 05:02:26 EDT 2019


On 13/08/19 7:11 AM, Marissa Russo wrote:
> This is my code:
> 
> import math
> 
> def get_numbers():
>      print("This program will compute the mean and standard deviation")
>      file1 = input("Please enter the first filename: ")
>      file2 = input("Please enter the second filename: ")
>      x = open(file1, "r")
>      y = open(file2, "r")
>      nums = x.readlines()
>      nums2 = y.readlines()
> 
>      return nums, nums2


Do you understand the concept of a "loop" - Code which is repeated as 
many times as necessary?

How many files must be opened, read, and then averaged?


> def to_ints(strings):
>      num_copy = []
>      for num in nums:
>          num_copy.append(float(num))
>      return num_copy
> 
>      return to_ints(nums), to_ints(nums2)

What is the purpose of this line, given that the previous line has 
returned to the calling code?

Have I missed something? When is to_ints() used?


> def mean(nums):
>      _sum = 0
>      return(sum(nums)/len(nums))
> 
> def main():
>      data = get_numbers()
>      m = mean(data[0])

Do you know what data[ 0 ] (or [ 1 ]) contains?
Might this knowledge be helpful?


>      m2 = mean(data[1])
>      print("The mean of the first file is: ", m)
>      print("The mean of the second file is: ", m2)
> main()
> 
> 
> This is the output of my updated code:
> 
> Traceback (most recent call last):
>    File "/Applications/Python 3.7/exercises .py", line 37, in <module>
>      main()
>    File "/Applications/Python 3.7/exercises .py", line 33, in main
>      m = mean(data[0])
>    File "/Applications/Python 3.7/exercises .py", line 29, in mean
>      return(sum(nums)/len(nums))
> TypeError: unsupported operand type(s) for +: 'int' and 'str'

What do you think "TypeError" means? Do you know the difference between 
an "int" and a "str[ing]"?

Given that both sum() and len() return numbers, what do you think is the 
"str"? Might this refer back to the earlier suggestion that you need to 
'see' the data being read?

-- 
Regards =dn


More information about the Tutor mailing list