I know I've created an infinite loop, but I'm not sure how to refine it to ask every single time the game ends, until the user DOESN'T enter "1" in which case it would print what is after the else statement. It sounds like a sentinel loop, but I'm just having a brain fart, would be great if I got a little advice! Thanks

Re: Restarting a program

The return value of input() is a string, which you are comparing against an int. Convert the result of your input value to an int before assigning to the restart variable.

Would you show me how using my code? its not compulsary homework or anything i just want to add it in to one of my assignments so i can learn it and use it in more of my programs, im still quite new and this is rather daunting

Re: Restarting a program

Posted 03 October 2012 - 05:47 AM

restart = '1' #Restart now equals the string 1, not the integer 1.
while restart == '1': #While restart is equal the the string 1:
restart = input("Would you like to play again? Please enter '1' for YES or '2' for NO: ")
print "Thanks for playing Mason's Who Wants To Be A Millionaire New Zealand Edition!"

That would be your fix using your precise code. Another way you could do it would be putting your entire program into a while loop where a variable named...let's say, playAgain was equal to true. Then at the end of your game there is a section of code that asks if the player would liek to play again. If they say yes, then nothing happens to playAgain as it is already true and your program restarts. If they say no then playAgain turns to False.

Example:

playAgain = True
answer = ''
while playAgain:
#Once upon a time I was falling in love.
#But now im only falling apart.
#There's nothing I can do.
#A total ecplise of the heart.
#After your program has been written...
answer = input('Would you like to play again? (yes/no):').lower()
if answer == 'n' or 'no':
playAgain = False
#End program, if the user typed y or yes or poop or anything else, playAgain remains true and the program starts over, if they type n or no then playAgain turns to false and the program exits the while loop.

I know there are better ways to do this, but the large while loop is what I typically do for any sort of game that requires a 'restart' function.

Re: Restarting a program

Posted 04 October 2012 - 10:59 PM

midknight51, on 03 October 2012 - 05:47 AM, said:

restart = '1' #Restart now equals the string 1, not the integer 1.
while restart == '1': #While restart is equal the the string 1:
restart = input("Would you like to play again? Please enter '1' for YES or '2' for NO: ")
print "Thanks for playing Mason's Who Wants To Be A Millionaire New Zealand Edition!"

That would be your fix using your precise code. Another way you could do it would be putting your entire program into a while loop where a variable named...let's say, playAgain was equal to true. Then at the end of your game there is a section of code that asks if the player would liek to play again. If they say yes, then nothing happens to playAgain as it is already true and your program restarts. If they say no then playAgain turns to False.

Example:

playAgain = True
answer = ''
while playAgain:
#Once upon a time I was falling in love.
#But now im only falling apart.
#There's nothing I can do.
#A total ecplise of the heart.
#After your program has been written...
answer = input('Would you like to play again? (yes/no):').lower()
if answer == 'n' or 'no':
playAgain = False
#End program, if the user typed y or yes or poop or anything else, playAgain remains true and the program starts over, if they type n or no then playAgain turns to false and the program exits the while loop.

I know there are better ways to do this, but the large while loop is what I typically do for any sort of game that requires a 'restart' function.

When i press 1 to play again it just says thanks for playing, its like its not actually looping

Re: Restarting a program

Posted 04 October 2012 - 11:41 PM

Ah, I see what I did, it is looping but because the print function is within the while loop, it will say that no matter what input the user provides. So you have to move that print function outside of the while loop. Or just get rid of it all together.

Updated code:

restart = '1' #Restart now equals the string '1' at the beginning of your code.
while restart == '1': #While restart is equal to the string '1':
#Your program here:
restart = input("Would you like to play again? Enter 1 for YES and 2 for NO.")
#Now the user enters 1 or 2. If the user enters 1, the while loops restarts, restarting your program.
#If they press 2 the program exits the while loop.
#Note the indentation of the next line to tell the shell it is now OUT of the while loop.
print "Thanks for playing Mason's Who Wants To Be A Millionaire New Zealand Edition!"

Re: Restarting a program

Posted 04 October 2012 - 11:53 PM

midknight51, on 04 October 2012 - 11:41 PM, said:

Ah, I see what I did, it is looping but because the print function is within the while loop, it will say that no matter what input the user provides. So you have to move that print function outside of the while loop. Or just get rid of it all together.

Updated code:

restart = '1' #Restart now equals the string '1' at the beginning of your code.
while restart == '1': #While restart is equal to the string '1':
#Your program here:
restart = input("Would you like to play again? Enter 1 for YES and 2 for NO.")
#Now the user enters 1 or 2. If the user enters 1, the while loops restarts, restarting your program.
#If they press 2 the program exits the while loop.
#Note the indentation of the next line to tell the shell it is now OUT of the while loop.
print "Thanks for playing Mason's Who Wants To Be A Millionaire New Zealand Edition!"

Wait. now it goes in at the start of the program? Here is my code so far then:

Re: Restarting a program

Posted 05 October 2012 - 12:03 AM

Does it work? The indention looks weird but as long as the indention is right in your code you should be fine. If it gives and error let me know. Also, I meant that your while loop goes at the begining of your actual program. Not the beginning of everything.

Since main() is your main program you would do.

while restart == '1':
main()

The reason you do this is mostly for clarity. Now when someone looks at your code they will know that you want main() to loop as long as restart == 1. When you put your loop at the beginning of everything, it looks like you only want your functions to be defined if restart == 1... make sense? This is just personal preference to me. If it works and you dont want to mess with it. Leave it be.

Re: Restarting a program

Posted 05 October 2012 - 12:37 AM

I wish I could man, I am at work. The DoD doesn't really like it when it's employees program on their computers.
I still see some problems with your code.

Take your while loop out from where it is now. It is only going to confuse you and others and possibly create more problems. You dont want a while loop BEFORE you define the functions you are going to use. In my C++ classes, my professors were really big onto how a code looks, so I am as well. This is typically how I organize my programs.

Imports (In your case, time)
Global Variables (In your case, restart only)
Definitions(In your case, main() and all of the questions() )
Main Program (This is where the magic happens.)

So after you have all of your imports, variables and definitions, you move onto your main program. Since your main program is... main()... this part is fairly short. This is also where your while loop should be.

Im not going to redo all of your code for you, you wont learn that way. But I will show you how I would have it setup.