Home > Software design >  the list in my code is not working properly
the list in my code is not working properly

Time:11-16

import random

def foo():
    list_of_odd_num = []
    for i in range (1, 10000, 2):
        list_of_odd_num.append(i)
    return list_of_odd_num    
def bar():
    list_of_uppercase_letters = []
    for k in range(1, 100):
        rand_num = random.randint(65, 90)
        letter = chr(rand_num)
        k = list_of_uppercase_letters.append(letter)

    return list_of_uppercase_letters           
def qux(any_list: list):
    i = 0
    while i < 20:
        for j in range (len(any_list)):
            rand_01 = random.randint(0,1)
            if rand_01 == 1:
                i = i   1
                any_list.insert(j, '?')               
    return any_list             
print(qux(bar()))

output: ['?', 'D', 'I', '?', 'Y', '?', 'X', 'Q', 'L', 'E', '?', '?', 'I', '?', 'H', '?', '?', '?', '?', 'E', '?', '?', 'B', '?', '?', '?', 'G', '?', '?', '?', '?', 'S', '?', 'U', 'W', 'I', 'G', '?', '?', 'L', '?', 'J', 'M', '?', '?', 'A', 'K', '?', 'X', '?', 'Y', 'J', 'L', 'S', '?', '?', '?', 'I', '?', 'Q', '?', 'S', 'L', 'R', '?', '?', 'L', '?', '?', '?', 'M', 'K', 'E', '?', 'B', '?', 'V', '?', 'I', 'L', '?', 'S', '?', '?', 'O', 'F', '?', 'O', 'S', 'J', '?', 'P', '?', 'X', '?', 'T', 'B', '?', 'Q', 'N', 'T', 'H', 'F', 'A', 'D', 'E', 'P', 'Y', 'Z', 'Q', 'M', 'X', 'I', 'H', 'Z', 'F', 'Q', 'G', 'Q', 'B', 'A', 'G', 'B', 'R', 'N', 'J', 'K', 'C', 'P', 'P', 'E', 'E', 'A', 'R', 'P', 'S', 'A', 'O', 'A', 'I', 'R', 'B', 'W', 'V', 'M', 'I', 'P']

i was trying to insert 20 "?"s in random indexes in a list that was given as an argument to the function qux() without overwriting the original items in the list by writng a while loop with a condition i < 20 and then in the for loop io kept adding up the i until its supposed to reach to 20 and then finish the while loop, but what ended up happening is that the program kept on printing "?"s that reached beyond the number 20 in any list unless it was was empty

CodePudding user response:

Your problem is that the i variable is defined locally in the for loop while the while loop will not recognise the reach of i=20 imediatly. So to solve this you could just put everyhing into a big while loop with:

while i == len(any_list) 20

CodePudding user response:

You have a for loop inside the while loop and only when the for loop terminates does your outer loop verify that i<20.

You should do something like this instead:

def qux(any_list: list):
    i = 0
    
    for j in range (len(any_list)):
        rand_01 = random.randint(0,1)
        if rand_01 == 1:
           i = i   1
           any_list.insert(j, '?')
           if i = 20:
               break    
    return any_list  

CodePudding user response:

The inner loop does not break when a value is added, thus, it may add multiple ? to the list before checking the outer loop condition (resulting in too many ?). You could fix the code like this:

import random

def foo():
    list_of_odd_num = []
    for i in range (1, 10000, 2):
        list_of_odd_num.append(i)
    return list_of_odd_num    
def bar():
    list_of_uppercase_letters = []
    for k in range(1, 100):
        rand_num = random.randint(65, 90)
        letter = chr(rand_num)
        k = list_of_uppercase_letters.append(letter)

    return list_of_uppercase_letters           
def qux(any_list: list):
    i = 0
    while i < 20:
        for j in range (len(any_list)):
            rand_01 = random.randint(0,1)
            if rand_01 == 1:
                i = i   1
                any_list.insert(j, '?')
                if i == 20:
                    break
              
    return any_list             
print(qux(bar()))

However, your code will not result in a uniform distribution of random ? and is unnecessarily complicated. you could do something like this:

import random

def foo():
    list_of_odd_num = []
    for i in range (1, 10000, 2):
        list_of_odd_num.append(i)
    return list_of_odd_num    
def bar():
    list_of_uppercase_letters = []
    for k in range(1, 100):
        rand_num = random.randint(65, 90)
        letter = chr(rand_num)
        k = list_of_uppercase_letters.append(letter)

    return list_of_uppercase_letters           
def qux(any_list: list):
    i = 0
    for i in range(20):
        any_list.insert(random.randint(0, len(any_list) - 1), '?')
              
    return any_list             
print(qux(bar()))

Also, foo and bar can be simplified:

import random
import string

def foo():
    # why do you need this though?
    return list(range(1, 1000, 2))

def bar():
    return random.choice(string.ascii_uppercase)

def qux(any_list: list):
    for i in range(20):
        any_list.insert(random.randrange(len(any_list)), '?')
    return any_list

  • Related