Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My code isn't excecuting
#1
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!
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply
#4
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?
Reply
#5
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
Reply
#6
Omg Snippssat!
Thank you sooo much ! With your help I could finally solve this problem!
Reply


Forum Jump:

User Panel Messages

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