Python Forum
How to use += after breaking for loop?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use += after breaking for loop?
#1
Hello,

I am new to the forum and new to Python and maybe i did not post in the right part of the forum.

I have this code:
import numpy as np

mlist = [8,7,5,2,1,4,5,6,8]

i = min(mlist)
print("low", i)

t = mlist.index(i)

o = len(mlist)
print("elements", o)

p = slice(0,t+1)
print("slash", mlist[p])


print(mlist[-t-1::-1])

add = 1 + 2 + 3

for x in mlist[-t-1::-1]:
    if x >= add:
     break 
    x += x                         // it return 2, 4, 10 in the output but should return 1, 3, 8
    print("stop",x)    
Any explaination why i cant have the right output is appreciated.

Thank you
buran write Dec-30-2021, 08:29 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
You've used x as your loop variable, so it is modified each time through the loop. If you want a variable to hold information across each cycle, you need to use a different variable.

On line 24 and 25 you modify and then print x. But when the loop runs again and goes back to line 21, your modifications are lost.

I'm guessing you're trying to do something like this:

total = 0
for x in mlist[-t-1::-1]:
    print(f"Checking {x} against {add}")
    if x >= add:
     break
    total += x
    print("stop",total)
Reply
#3
First i did that but i initialized my variable inside de loop. I woke up this morning with the same idea.
Thanks for your reply and solution.

(Dec-30-2021, 09:28 PM)bowlofred Wrote: You've used x as your loop variable, so it is modified each time through the loop. If you want a variable to hold information across each cycle, you need to use a different variable.

On line 24 and 25 you modify and then print x. But when the loop runs again and goes back to line 21, your modifications are lost.

I'm guessing you're trying to do something like this:

total = 0
for x in mlist[-t-1::-1]:
    print(f"Checking {x} against {add}")
    if x >= add:
     break
    total += x
    print("stop",total)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ChromeDriver breaking Python script genericusername12414 1 218 Mar-14-2024, 09:39 AM
Last Post: snippsat
  Openpyxl module breaking my code EnderSM 5 985 May-26-2023, 07:26 PM
Last Post: snippsat
  breaking out of nested loops Skaperen 3 1,175 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  How to fix while loop breaking off raphy11574 3 2,126 Oct-12-2021, 12:56 PM
Last Post: deanhystad
  breaking a large program into functions, not acting as expected Zane217 9 2,932 Sep-18-2021, 12:37 AM
Last Post: Zane217
  Need to run 100+ Chrome’s with Selenium but can’t get past ~15 without breaking imillz 0 1,322 Sep-04-2021, 04:51 PM
Last Post: imillz
Exclamation Help in breaking bytes into bits through masking and shifting kamui123 9 4,435 Jan-11-2021, 07:42 AM
Last Post: kamui123
  breaking outof nexted loops Skaperen 4 3,061 Feb-07-2020, 02:04 AM
Last Post: Skaperen
  Breaking While Loop JustWonderful 4 3,011 Oct-28-2019, 01:12 AM
Last Post: JustWonderful
  (Python help) Change in logic not breaking 'while' loop? btcg2807 1 1,862 Sep-18-2019, 09:43 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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