Python Forum
My code isn't excecuting - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: My code isn't excecuting (/thread-29284.html)



My code isn't excecuting - saratha - Aug-26-2020

Hello,
I have excecuted my function but it doesn't run. Does anyone know why?
def randword():
	from random import choice
	from string import ascii_lowercase
	n = 10
	word_list = []
	another_random = ''.join(choice(ascii_lowercase) for r in range(n))
	for char in range(10):
		while True:
			n += 10
	rand_word = ''.join(choice(ascii_lowercase)for i in range(n))
	lst = list(rand_word)
	print(''.join(lst))
Thank you!


RE: My code isn't excecuting - buran - Aug-26-2020

That's just a function, but assuming you actually call it, there is infinite loop on lines 8 and 9. You keep increasing n and never break out of the loop


RE: My code isn't excecuting - snippsat - Aug-26-2020

You never call the function also randword() as last line not inside function.
Also the code will lock up and not work,here is a working improved version.
from random import choice
from string import ascii_lowercase

def randword(word_lenght=10):
    rand_word = ''.join(choice(ascii_lowercase)for i in range(word_lenght))
    return rand_word

if __name__ == '__main__':
    print(randword()) # Default use lenght of 10
    print(randword(20))
Output:
ncuxdhorxp zocynvfdkvobwkrqsdqx



RE: My code isn't excecuting - saratha - Aug-26-2020

Hey snippsat,
Thanks for the help, but my function is to increment 10 characters for a hundred times each time like these. For example:
fwevervrbh(10)
pugjyshkuhswersdghjy(+10)(20)
efecerjvrugyvrivjnudmihdnmoudg(+10)(30)

Might you have a suggestion for that?


RE: My code isn't excecuting - snippsat - Aug-26-2020

sarath Wrote:Might you have a suggestion for that?
You should give it try Wink

The range() function can do step range(start, stop, step).
>>> for i in range(10, 50, 10):
...     print(i)
... 
10
20
30
40
The can just add that to my code.
from random import choice
from string import ascii_lowercase

def randword(word_lenght=10):
    rand_word = ''.join(choice(ascii_lowercase) for i in range(word_lenght))
    return rand_word

if __name__ == '__main__':
    for i in range(10, 50, 10):
        print(randword(i))
Output:
rksiqpfwbz axzpwdmbbsvktfluxzej fwppdyvjobgdacfwbdjyfmvyfxagwv ebvnoazsmmmlvksabhyewqfoxvevwvqqhrwyqxwv



RE: My code isn't excecuting - saratha - Aug-26-2020

Omg Snippssat!
Thank you sooo much ! With your help I could finally solve this problem!