Ask your own question, for FREE!
Computer Science 22 Online
OpenStudy (anonymous):

I need to make a recursive function to add positive integers inputted by a user into a list. My code works, however, I don't know how to end the loop if the user enters an exclamation mark without crashing because of conflict in type: def listsum(numList): theSum = 0 for i in numList: theSum = theSum + i return theSum def main(): dlist=[] add='' while not add=="!": add= eval(input("Enter an integer:\n")) dlist.append(add) print(listsum(dlist)) main()

OpenStudy (anonymous):

eval is defining the exclamation mark as an EOF. if you give me a moment i'll see if I can find a work around

OpenStudy (anonymous):

eval is just making the input a number so that the sum function can work, however if the input is an exclamation mark it crashes because it is not an integer or float, I don't know how to fix that

OpenStudy (anonymous):

def listsum(numList): theSum = 0 for i in numList: theSum = theSum + i return theSum def main(): dlist=[] add='' while 1: add = input(u"Enter an integer:\n") if add.isnumeric(): dlist.append(int(add)) elif add == "!": break print(listsum(dlist)) main()

OpenStudy (anonymous):

this worked for me Enter an integer: 5 Enter an integer: 5 Enter an integer: 5 Enter an integer: 5 Enter an integer: 5 Enter an integer: 50 Enter an integer: ! 75

OpenStudy (anonymous):

Makes sense, Thank you very much. Just another question, how can you print a list without printing the brackets etc for example: print(listA[::-1]) ['mat', 'the', 'on', 'sat', 'dog', 'the']

OpenStudy (anonymous):

I think you're going to need to iterate through the list and print each element instead of all elements at once. item[0] item[1] ect.

OpenStudy (anonymous):

I'm sure there is a more efficient way to do it, I will play around and let you know

OpenStudy (anonymous):

okay

OpenStudy (anonymous):

You just have to use a join function: print(" ".join(string[::-1]))

OpenStudy (anonymous):

One last thing (sorry I know you don't program in python), Python has a count function but I have to create my own, this is what I have but it keeps returning the value 6. (it is supposed to count how many times a word is repeated in the list): def COUNT(terms,query): counter=0 for query in terms: counter=counter+1 print("The term {} occurs {} times".format(query,counter))

OpenStudy (anonymous):

okay, def COUNT(term, query): ctr = 0 for i in range(len(term)): if term[i] == query: ctr += 1 print("The term {} occurs in {} times".format(query, ctr)) term = ['check', 'dog', 'red', 'check', 'orange', 'check'] COUNT(term, 'check') that works perfectly

OpenStudy (anonymous):

Thank you so much

OpenStudy (anonymous):

you're welcome

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!