0

so i have func and i called it with lcg. the func have return value and i got the value i want.

6 7 2 11 14 15 10 3 6 7 2 11 14 15 10 3 # this is the result from func lcg

but after i called the func and print tes5, i just get result 3

i don't know what wrong, so please help me to fix it

here's the code:

def lcg(n, m):
    a = 11
    c = 5
    dec = n
    m = m * 4
    for i in range(0,m):
        LCG = ((a*dec)+c) % m 
        print(LCG, end= ' ') # result = 6 7 2 11 14 15 10 3 6 7 2 11 14 15 10 3
        dec = LCG
        
    return LCG

tes5 = lcg(3, 4)
print(tes5)

print(tes5) # result = 3
Mechanic Pig
  • 6,756
  • 3
  • 10
  • 31
HelpMe
  • 21
  • 6
  • In the end, you are returning only one value you should try returning a list instead – THUNDER 07 Sep 22 '22 at 10:02
  • how to do it? @THUNDER07 – HelpMe Sep 22 '22 at 10:03
  • 1
    li = [] at start , in each iteration li.append(LCG) return li at end – THUNDER 07 Sep 22 '22 at 10:04
  • yeah it can, but can i remove the array? cause i don't need it @THUNDER07 – HelpMe Sep 22 '22 at 10:06
  • if you remove the array don't expect tes5 to store all values – THUNDER 07 Sep 22 '22 at 10:08
  • The main issue here is that you were using a print in the loop without \n to print all interim results; you never saved those. The function correctly returns only the last value actually calculated. – Roland Sep 22 '22 at 10:11
  • can i saved without using array? @Roland – HelpMe Sep 22 '22 at 12:05
  • The array, list to be precise, is the simplest way to do this in Python in a case like this, even if you only want to print the result. Create the empty list as suggested before your loop, use append during the loop, and return the list at the end, or use return(" ".join(li)) if you want a string as the result. – Roland Sep 22 '22 at 13:27

0 Answers0