0

I am using python 2.6.6

I am simply trying to restart the program based on user input from the very beginning. thanks

import random
import time
print "You may press q to quit at any time"
print "You have an amount chances"
guess = 5
while True:
    chance = random.choice(['heads','tails'])
    person = raw_input(" heads or tails: ")
    print "*You have fliped the coin"
    time.sleep(1)
    if person == 'q':
         print " Nooo!"
    if person == 'q':
        break   
    if person == chance:
        print "correct"
    elif person != chance:
        print "Incorrect"
        guess -=1
    if guess == 0:
        a = raw_input(" Play again? ")
        if a == 'n':
            break
        if a == 'y':
            continue

#Figure out how to restart program

I am confused about the continue statement. Because if I use continue I never get the option of "play again" after the first time I enter 'y'.

Tarrant
  • 179
  • 2
  • 4
  • 9

4 Answers4

2

Use a continue statement at the point which you want the loop to be restarted. Like you are using break for breaking from the loop, the continue statement will restart the loop.

Not based on your question, but how to use continue:

while True: 
        choice = raw_input('What do you want? ')
        if choice == 'restart':
                continue
        else:
                break

print 'Break!' 

Also:

choice = 'restart';

while choice == 'restart': 
        choice = raw_input('What do you want? ')

print 'Break!' 

Output :

What do you want? restart
What do you want? break
Break!
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
user225312
  • 126,773
  • 69
  • 172
  • 181
  • See also: http://stackoverflow.com/questions/1420029/how-to-break-out-of-a-loop-from-inside-a-switch/1420100#1420100 – Dave Jarvis Dec 29 '10 at 20:00
  • @Dave: Interesting, I never looked at `while True` this way. But is it a bad practise in Python? I know the article mentions the fact being independent of languages, but just wondering... – user225312 Dec 29 '10 at 20:03
  • @Dave: Somehow I still find `while True` better, but we will wait for others to give their opinions. – user225312 Dec 29 '10 at 20:07
  • @sukhbir: I added a variation to your answer showing a way to eliminate `while True`. Less typing. Faster to read. Terminating condition cannot be overlooked. Eliminates the `continue` redundancy. – Dave Jarvis Dec 29 '10 at 20:12
  • Yes. Make sure you indent the code properly, and then use `continue`. The loop will be called again. – user225312 Dec 29 '10 at 20:14
  • @sukhbir: As a story, mine reads, "Initialize the user's choice. Loop while the user's choice is the word 'restart'. Ask the user to change the choice variable's value. The end." Yours reads, "Let's loop forever! Ask the user to change the choice variable's value. If they changed the value to 'restart' then we're going to continue looping (forever). Otherwise, we're not going to loop forever, so forget I said anything about looping forever. The end." – Dave Jarvis Dec 29 '10 at 20:20
1

I recommend:

  1. Factoring your code into functions; it makes it a lot more readable
  2. Using helpful variable names
  3. Not consuming your constants (after the first time through your code, how do you know how many guesses to start with?)

.

import random
import time

GUESSES = 5

def playGame():
    remaining = GUESSES
    correct = 0

    while remaining>0:
        hiddenValue = random.choice(('heads','tails'))
        person = raw_input('Heads or Tails?').lower()

        if person in ('q','quit','e','exit','bye'):
            print('Quitter!')
            break
        elif hiddenValue=='heads' and person in ('h','head','heads'):
            print('Correct!')
            correct += 1
        elif hiddenValue=='tails' and person in ('t','tail','tails'):
            print('Correct!')
            correct += 1
        else:
            print('Nope, sorry...')
            remaining -= 1

    print('You got {0} correct (out of {1})\n'.format(correct, correct+GUESSES-remaining))

def main():
    print("You may press q to quit at any time")
    print("You have {0} chances".format(GUESSES))

    while True:
        playGame()
        again = raw_input('Play again? (Y/n)').lower()
        if again in ('n','no','q','quit','e','exit','bye'):
            break
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
0

You need to use random.seed to initialize the random number generator. If you call it with the same value each time, the values from random.choice will repeat themselves.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

After you enter 'y', guess == 0 will never be True.

Kabie
  • 10,489
  • 1
  • 38
  • 45
  • Thanks...I just used the continue statement like suggested above and got that part of it working...however I will still consider all previous answers thanks. – Tarrant Dec 30 '10 at 18:45