Home > Software design >  Find negative Numbers in list and change | Python
Find negative Numbers in list and change | Python

Time:08-06

I want to sort out all Numbers under 0 in a List and add to the numbers under 0 500.

import random
from turtle import st

startFrame = random.randint(0, 200)

print(startFrame)

start = []

for i in range(startFrame -125, startFrame):
    
    start.append(i)

print(start)

for Frame in start:
    
    if Frame > 0:
        
        Frame   500
        
print(start)

Did anyone can find out why its not working?

CodePudding user response:

Just do the following:

for i in range(len(start)):
    if start[i] < 0:
        start[i]  = 500
print(start)

CodePudding user response:

You need to save your addition in a new list and output that.

import random
from turtle import st

startFrame = random.randint(0, 200)

print(startFrame)

start = []

for i in range(startFrame -125, startFrame):
    
    start.append(i)


print(start)

start2 =[]
for Frame in start:
    
    if Frame < 0:
        
        start2.append(Frame   500)
        
        
print(start2)

CodePudding user response:

The adjustment to your Frame variable has no effect on the list contents.

You could do this:

from random import randint
    
r = randint(0, 200)
start = list(map(lambda x: x if x > 0 else x   500, range(r-125, r)))
print(start)
  • Related