![]() |
Error in loops, new to Python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Error in loops, new to Python (/thread-2890.html) |
Error in loops, new to Python - jhall710 - Apr-17-2017 I just recently got involved with trying to learn python and am actually trying to teach myself to help me get a better job in the GIS field. Have tried many ways to get the right code for these two problems. I'll post the questions along with my code. I'm thinking it is a syntax issue in 1 or 2 of my lines. Thanks for the help and please remember I am just a beginner! Question1:
list = [4,6,12,9,1,2] S = 0 C = 0 X = 999 while list[0]: S = S + X C = C + 1 if C > 0: A = S/C print(A) else: print("No Answer") Thinking I may have a syntax issue or error in my loop Question 2:
list = [3,17,4,19,8,2] I = 0 X = 0 G = list() while I < 6: list(I) > G G = list(I) X = I I = I + 1 print(G) print(X) this one threw me for a spin Appreciate any help! RE: Error in loops, new to Python - nilamo - Apr-18-2017 (Apr-17-2017, 10:42 PM)jhall710 Wrote:list = [4,6,12,9,1,2] S = 0 C = 0 X = 999 while list[0]: S = S + X C = C + 1 You're incrementing X and C, as long as list[0] is a Truthy value... but you're never modifying any of the elements in list, so list[0] will always be Truthy. Basically, you're doing some unrelated thing, while purposefully looping infinitely. Also, list is a terrible variable name, since it's also a built-in function (and type). Maybe call it input or items instead?
RE: Error in loops, new to Python - jhall710 - Apr-18-2017 Thanks for the tip! I'm still pretty confused with the looping though.. I kind of figured my while statement is whats incorrect but not sure what to input instead of 0 so the program stops once the list is empty RE: Error in loops, new to Python - nilamo - Apr-18-2017 (Apr-18-2017, 02:27 AM)jhall710 Wrote: so the program stops once the list is empty while list: would do that just fine. The problem, is that you're never taking anything out of the list, so it'll never be empty.
RE: Error in loops, new to Python - jhall710 - Apr-18-2017 Ok I see what you mean. Going to try and do a little more research definitely pretty lost though. There was a hint given as a set up but I dont know how to apply it. #### Set-up #### inputValues = [] while True: t = int(input()) if t == -1: break inputValues.append(t) RE: Error in loops, new to Python - nilamo - Apr-18-2017 It's pretty simple. The while loop keeps going until there's no elements left. Which is forever, unless you actually remove things from the list. Something like: >>> items = [4, 34, 6, 7] >>> total = 0 >>> while items: ... total += items.pop() ... >>> total 51 >>> items [] RE: Error in loops, new to Python - sparkz_alot - Apr-18-2017 (Apr-18-2017, 02:18 AM)nilamo Wrote: Maybe call it input or items instead? Maybe "input" is not a good choice either :-D RE: Error in loops, new to Python - smbx33 - Apr-23-2017 I made minor modifications to the existing code and provided explanation. list_ = [4,6,12,9,1,2] s = 0 c = 0 x = 999 while len(list_) >0: # here you had list[0] I think you wanted to stop when length reached 0 for amount in list_: # for loop will go through the list_ of numbers one amount at a time x = amount # the for loop will give individual amounts to x variable. s += x # same as your code S = S + X c += 1 # same as your code C = C + 1 list_.remove(amount) #this is to remove items from your list! # you don't need to check the counter. so the if statement could be deleted or reworded so if the program is run and no items are on the list it says NO ITEMS! else it prints A RE: Error in loops, new to Python - nilamo - Apr-24-2017 Modifying a list while you're in the middle of iterating over that same list is very dangerous. RE: Error in loops, new to Python - smbx33 - Apr-24-2017 (Apr-24-2017, 03:43 AM)nilamo Wrote: Modifying a list while you're in the middle of iterating over that same list is very dangerous. you are defiantly correct! (I had to look it up) I still don't understand why as it gives the desired result in this case. |