Jump to content
  • Advertisement
Sign in to follow this  
Durakken

Python Program is freezing up...

This topic is 3404 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is probably something stupid. The program does what it is supposed to do and then it freezes and won't allow the last line to execute and end the program btw this is for a class assignment...which is why it looks like it is. It's done other than this and nothing like this is covered in the book as far as i can tell.
import random

phrase = ("There", "was", "never", "a", "doubt", "that", "you", "would", "have",
        "the", "best", "of", "whatever", "that", "could", "have", "been",
        "had", "before", "we", "finished", "off", "your", "puny", "limited",
        "and", "short", "life")

used = []

for count in range(len(phrase)):
    word = random.choice(phrase)
    while word in used:
        word = random.choice(phrase)
    print word,
    if(count == len(phrase)-1):
        print "!"
    used.append(word)

raw_input(" ")

Share this post


Link to post
Share on other sites
Advertisement
Based on a quick run through the debugger, your problem appears to be in lines 13 and 14, the while loop. As you put more and more words into the used list, your chances of randomly selecting an unused word from the list become smaller and smaller, causing your program to run for a long time until it finally picks all words from the list by chance. Here is an updated version that simply removes used words from the list, rather than storing them in a separate location (make sure you press Enter at the end of this new program to end it, because of raw_input()):


import random

phrase = ["There", "was", "never", "a", "doubt", "that", "you", "would", "have",
"the", "best", "of", "whatever", "that", "could", "have", "been",
"had", "before", "we", "finished", "off", "your", "puny", "limited",
"and", "short", "life"]

for count in range(len(phrase)):
word = random.choice(phrase)
print word,
if(count == len(phrase)-1):
print "!"
phrase.remove(word)

raw_input(" ")

Share this post


Link to post
Share on other sites
Your program doesn't freeze, it just gets stuck in an infinite loop somewhere. Try to analyze your code carefully to see the bottleneck. Perhaps place a few additional print's so you can see what's going on. Hint: it has to do with how you're selecting unused words.

Share this post


Link to post
Share on other sites
Purely for the purposes of education, the whole program could pretty much be replaced with this:
import random
shuffled = list(phrase)
random.shuffle(shuffled)
print ' '.join(shuffled) + '!'
raw_input(" ")


It's worth noting why you have to use the list constructor, and why random.shuffle is better than keeping your own 'used' list. And also it's worth looking up the function that would do something similar if you didn't want all the elements of 'phrase', just a subset.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!