Question

Hello, This is what i wrote: def squareEach(nums): answer = [] for num in nums: answer.append(num*num)...

Hello,
This is what i wrote:


def squareEach(nums):
answer = []
for num in nums:
answer.append(num*num)
return answer

def sumList(nums):
answer = 0
for num in nums:
answer += num
return answer

def toNumbers(nums):
new_nums = []
for i in nums:
new_nums.append(int(i))
return new_nums

def main():
fileName = input("What file are the numbers in? ")
sum = 0
with open(fileName, 'r') as infile:
for line in infile:
nums.append(line)
print(nums)
nums = toNumbers(line)
print(nums)
squares = squareEach(nums)
print(nums)
sum += sumList(squares)
print("The sum of the squares is: ",sum)

main()

BUT, This is what i get:

Traceback (most recent call last):
File "C:\Users\Manlai\Documents\Python programs\Assignment 4.py", line 33, in <module>
main()
File "C:\Users\Manlai\Documents\Python programs\Assignment 4.py", line 24, in main
nums.append(line)
UnboundLocalError: local variable 'nums' referenced before assignment
>>>

Please Help me <3

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def squareEach(nums):

answer = []

for num in nums:

answer.append(num*num)

return answer

def sumList(nums):

answer = 0

for num in nums:

answer += num

return answer

def toNumbers(nums):

new_nums = []

for i in nums:

new_nums.append(int(i))

return new_nums

def main():

fileName = input("What file are the numbers in? ")

sum = 0

nums=[]

with open(fileName, 'r') as infile:

for line in infile:

nums.append(line)

print(nums)

nums = toNumbers(line)

print(nums)

squares = squareEach(nums)

print(nums)

sum += sumList(squares)

print("The sum of the squares is: ",sum)

main()

Add a comment
Know the answer?
Add Answer to:
Hello, This is what i wrote: def squareEach(nums): answer = [] for num in nums: answer.append(num*num)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • convert the following code from python to java. def topkFrequent(nums, k): if not nums: return [...

    convert the following code from python to java. def topkFrequent(nums, k): if not nums: return [ ] if len(nums) == 1: return nums [0] # first find freq freq dict d = {} for num in nums: if num in d: d[num] -= 1 # reverse the sign on the freq for the heap's sake else: d[num] = -1 h = [] from heapq import heappush, heappop for key in di heappush(h, (d[key], key)) res = [] count = 0...

  • THIS IS THE CODE I AM TRYING TO RUN code # def main():       #Reading a as...

    THIS IS THE CODE I AM TRYING TO RUN code # def main():       #Reading a as an integer       a = int(input("Enter value for a: "))       #reading b as an interger       b = int(input("Enter value for b: "))       #reading n as an integer       n = int(input("Enter value for n: "))       # displaying if a and b are congruent modulo n, this is True if a and b       #both returns same remainder when divided by n       if a % n == b...

  • I am having a little trouble with my Python3 code today, I am not sure what...

    I am having a little trouble with my Python3 code today, I am not sure what I am doing wrong. Here are the instructions: and here is my code: update: I have seen I did not close x in sumFile and I am still only getting a 2/10 on the grader. any help appreciated. Lab-8 For today's lab you going to write six functions. Each function will perform reading and/or write to a file Note: In zybooks much like on...

  • Python Problem Hello i am trying to finish this code it does not save guesses or...

    Python Problem Hello i am trying to finish this code it does not save guesses or count wrong guesses. When printing hman it needs to only show _ and letters together. For example if the word is amazing and guess=a it must print a_a_ _ _ _ not a_ _ _ _ and _a_ _ _ separately or with spaces. The guess has a maximum of 8 wrong guesses so for every wrong guess the game must count it if...

  • I have this code for python: num = int(input()) if num == 0: print('No change') else:...

    I have this code for python: num = int(input()) if num == 0: print('No change') else: dol = num // 100 num %= 100 Quarters = num // 25 num %= 25 Dimes = num // 10 num %= 10 Nickels = num // 5 num %= 5 Pennies = num if dol > 0: if dol == 1: print('1 Dollar') if Dollars > 1: print(dol,'Dollars') if Quarters > 0: if Quarters == 1: print('1 Quarter') if Quarters > 1:...

  • Write a python program write a function numDigits(num) which counts the digits in int num. Answer...

    Write a python program write a function numDigits(num) which counts the digits in int num. Answer code is given in the Answer tab (and starting code), which converts num to a str, then uses the len() function to count and return the number of digits. Note that this does NOT work correctly for num < 0. (Try it and see.) Fix this in two different ways, by creating new functions numDigits2(num) and numDigits3(num) that each return the correct number of...

  • Hello I have a file containing a pin, account name, and balance all on one line....

    Hello I have a file containing a pin, account name, and balance all on one line. I need to append the pin to the list and the name plus balance to the dictionary. Here is my code for the function below. def atm(inf): infile = open(inf,"r") bigbank={} pin=[] for line in infile: if line[0:3] in infile: pin.append(line[0:3]) #bigbank.update(line[4:])#appendDict????? else: #line(4,len(line)) in infile: name,score = line.split(":") bigbank[name] = int(score) return (pin,bigbank) print(pin,bigbank) inf = input('Insert?') #call the function pin1, bigbank1 =...

  • What will display after the following code is executed?. det magic (num): answer = num +...

    What will display after the following code is executed?. det magic (num): answer = num + 2 . 10 return answer def main(): print("The answer is" + str (magic(5))) main() The statement will cause a syntax error. 100 70 25 MacBook Air

  • My module page is correct but I don't believe I am using the main() function properly....

    My module page is correct but I don't believe I am using the main() function properly. One of my .py files is supposed to contain my definitions, which is the module file. My second .py file is supposed to properly call a main function that does what my program asks it to: First name, last name, age, and respond based off of input. For some reason I am struggling with using the main() function so that my program works. Any...

  • PYTHON PROGRAMMING: I have this program right now where it allows users to choose from a...

    PYTHON PROGRAMMING: I have this program right now where it allows users to choose from a category(pulling from the file). Then it will print the University or people from that text file. What I want to do next on my code is for users to search for a specific string from that file and it will display both the University and People that have that matching string. It can be the whole word or part of the string from that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT