Need help with exiting a loop. So my problem is that entering a '.' in response to the raw_input prompt should end the game, but I think it is only exiting either the if or while loop but not the for loop. How can I exit the for loop? I tried implementing what was called a boolean approach I found in stackoverflow with the gameon variable, but it did not work. Here is my code: # Problem #4: Playing a hand # def play_hand(hand, word_list): """ Allows the user to play the given hand, as follows: * The hand is displayed. * The user may input a word. * An invalid word is rejected, and a message is displayed asking the user to choose another word. * When a valid word is entered, it uses up letters from the hand. * After every valid word: the score for that word is displayed, the remaining letters in the hand are displayed, and the user is asked to input another word. * The sum of the word scores is displayed when the hand finishes. * The hand finishes when there are no more unused letters. The user can also finish playing the hand by inputing a single period (the string '.') instead of a word. hand: dictionary (string -> int) word_list: list of lowercase strings """ # TO DO ... display_hand(hand) runningscore=0 for x in hand: gameon= True while hand[x]>0: input1=raw_input('Please play a word') if input1=='.': print 'Game over. Final score:', runningscore gameon=False break elif is_valid_word(input1, hand, word_list)== False : print 'Invalid word, please retry' elif is_valid_word(input1, hand, word_list)== True: runningscore+=get_word_score(input1, 7) update_hand(hand, input1) display_hand(hand) print runningscore print 'Final score', runningscore hand=deal_hand(7) play_hand(hand, word_list)
So you've discovered that your break breaks the while loop (not the if statement, that's not a loop). What happens next is that the for loop does not know it should stop, so it continues with the next value in the hand. So the big problem is, how can you tell the for loop to stop? Using the `gameon` variable is a very good start. However, at the moment you're only giving it a value, not using the value. Try writing something where the for loop checks if the while loop finished normally or if it finished with a break (you can use `gameon` for that). If it finished with a break, you can also break out of the for loop.
So here is my new code. It works, but I don't know why. def play_hand(hand, word_list): """ Allows the user to play the given hand, as follows: * The hand is displayed. * The user may input a word. * An invalid word is rejected, and a message is displayed asking the user to choose another word. * When a valid word is entered, it uses up letters from the hand. * After every valid word: the score for that word is displayed, the remaining letters in the hand are displayed, and the user is asked to input another word. * The sum of the word scores is displayed when the hand finishes. * The hand finishes when there are no more unused letters. The user can also finish playing the hand by inputing a single period (the string '.') instead of a word. hand: dictionary (string -> int) word_list: list of lowercase strings """ # TO DO ... display_hand(hand) runningscore=0 gameon= True for x in hand: while hand[x]>0 and gameon==True: input1=raw_input('Please play a word') if input1=='.': print 'Game over. Final score:', runningscore gameon=False #break elif is_valid_word(input1, hand, word_list)== False : print 'Invalid word, please retry' elif is_valid_word(input1, hand, word_list)== True: runningscore+=get_word_score(input1, 7) update_hand(hand, input1) display_hand(hand) print runningscore print 'Final score', runningscore Doesn't changing gameon to false in response to a '.' only exit the while loop? Why is moving the gameon above the for statement important? THANK YOU!
Join our real-time social learning platform and learn together with your friends!