Python Forum
Error in loops, new to Python
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in loops, new to Python
#1

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:
  • On the line labeled Input write the following numbers: 4 6 12 9 1 2
  • Draw a box, label the box S, write the number 0 in box S
  • Draw a box, label the box C, write the number 0 in box C
  • Draw a box, label the box X write the number 999 in box X
  • If there are still numbers on the line labeled Input then do the next instruction, otherwise do instruction 10
  • Replace the number in box X with the the next number on the line labeled Input, erase from the Input line the number that was copied to box X
  • Replace the number in box S with the sum of the number that is currently in box S plus the number in box X
  • Replace the number in box C with the number that is one more than the number currently in box C
  • If the number in box C is greater than 0 then do the next instruction, otherwise do instruction 14
  • Draw a box, label the box A, write the value in box S divided by the value in box C in box A
  • Write the number in box A on the line labeled Output Go to instruction 15 Write the words "No answer" on the Output line

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:
  • Put the following numbers in boxes 0 - 5, one number per box: 3, 17, 4, 19, 8, 2
  • Draw a box, label the box I, write the number 0 in box I
  • Draw a box, label the box X, write the number 0 in box X
  • Draw a box, label the box G, copy the number from numbered box 0 to box G
  • If the number in box I is less than 6 then do the next instruction, otherwise do instruction 10
  • If the number in the numbered box whose number is the same as the number in box I is greater than the number in box G then do the next instruction, otherwise do instruction 8
  • Replace the number in box G with the value currently in the numbered box whose number is the same as the number in box I
  • Replace the number in box X with the number that is in box I
  • Replace the number in box I with the number that is one more than the number currently in box I
  • Go back and do instruction 4 again Write the number in box G on the line labeled Output Write the number in box X on the line labeled Output
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!
Reply
#2
(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?
Reply
#3
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
Reply
#4
(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.
Reply
#5
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)
Reply
#6
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
[]
Reply
#7
(Apr-18-2017, 02:18 AM)nilamo Wrote: Maybe call it input or items instead?

Maybe "input" is not a good choice either :-D
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#8
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
Reply
#9
Modifying a list while you're in the middle of iterating over that same list is very dangerous.
Reply
#10
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New to python! Loops Seeley307 3 52,441 May-15-2020, 02:27 PM
Last Post: ibreeden
  Python for loops giving error Petrus 12 5,408 Jan-09-2019, 08:02 AM
Last Post: Petrus

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020