Restarting a program

I'm trying to restart this text-game I made based on user input as

Page 1 of 1

14 Replies - 10938 Views - Last Post: 05 October 2012 - 12:37 AM Rate Topic: -----

#1 cmwise  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 169
  • Joined: 14-February 09

Restarting a program

Posted 30 March 2009 - 05:08 PM

Hey, I've created a text based game that I've defined as "game()" I have my "main()" function running the program and containing a restart option. So far my code looks like this:

def main():
	game()
	restart = input("Would you like to play again? Please enter '1' for YES or '2' for NO: ")
	while restart == 1:
		game()
		print restart
	else:
		print "Thanks for playing!"
				
main()



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 :D

Is This A Good Question/Topic? 0
  • +

Replies To: Restarting a program

#2 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Restarting a program

Posted 30 March 2009 - 05:54 PM

You can do it-
def main():
	game()
	while True:
		restart = input("Would you like to play again? Please enter '1' for YES or '2' for NO: ")
		if restart==1:
			game()
		else:
			print "Thanks for playing!"
			break
			   
main()


Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Restarting a program

Posted 30 March 2009 - 05:58 PM

Not bad, really. No real way to answer this than to show it.

def main():
	while True:
		game()
		restart = input("Would you like to play again? Please enter '1' for YES or '2' for NO: ")
		if not restart == 1:
			break
	print "Thanks for playing!"



Here what we're doing is intentionally making an infinite loop and then explicitly escaping. This has two elements you want; only calling game() once and not asking until after the first play.

A variant is to initialize the loop check to true going in:
def main():
	restart = 1
	while restart == 1:
		game()
		restart = input("Would you like to play again? Please enter '1' for YES or '2' for NO: ")
	print "Thanks for playing!"



It's more style than anything else. This basic question comes up almost daily in the C forum, btw. ;)

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 cmwise  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 169
  • Joined: 14-February 09

Re: Restarting a program

Posted 30 March 2009 - 06:07 PM

Ah, thanks a lot you two. It's running perfectly now, don't know why I didn't catch this before! :-\
Was This Post Helpful? 0
  • +
  • -

#5 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Restarting a program

Posted 30 March 2009 - 06:11 PM

Thats great!
Enjoy it!
Was This Post Helpful? 0
  • +
  • -

#6 Osbornezo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 12

Re: Restarting a program

Posted 29 September 2012 - 05:32 AM

Sorry about the bump but im wondering where i'm going wrong, when i press 1 to continue it just comes up again, here is my code so far
def Question10():

    print'\nName the body of water that separates the North and South Islands'
    print '\nA)Bass Strait'
    print '\nB)Tasman Sea'
    print '\nC)Foveaux Strait'
    print '\nD)Cook Strait'
    ans10=raw_input ('Answer: ')
    if ans10 == 'A' or ans10=='a':
        print 'Wrong!'
    elif ans10 == 'B'or ans10=='b':
        print 'Wrong!'
    elif ans10 == 'C'or ans10== 'c':
        print 'Wrong!'
    elif ans10 == 'D'or ans10== 'd':
        print 'Correct! The body of water that separates the North and South Islands is the Cook Strait!'
        time.sleep(1)
        end = round(start - time.time(), 2)
        print "you completed this quiz in " + str(end) + " seconds."



    restart = 1

    while restart == 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!"


Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: Restarting a program

Posted 29 September 2012 - 08:56 AM

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.
Was This Post Helpful? 0
  • +
  • -

#8 Osbornezo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 12

Re: Restarting a program

Posted 29 September 2012 - 06:20 PM

View PostJackOfAllTrades, on 29 September 2012 - 08:56 AM, said:

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 :P
Was This Post Helpful? 0
  • +
  • -

#9 midknight51  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 47
  • Joined: 25-September 12

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.
Was This Post Helpful? 0
  • +
  • -

#10 Osbornezo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 12

Re: Restarting a program

Posted 04 October 2012 - 10:59 PM

View Postmidknight51, 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
Was This Post Helpful? 0
  • +
  • -

#11 midknight51  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 47
  • Joined: 25-September 12

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!" 


Was This Post Helpful? 0
  • +
  • -

#12 Osbornezo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 12

Re: Restarting a program

Posted 04 October 2012 - 11:53 PM

View Postmidknight51, 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:

# Mason Osborne
# 10 question multi choice answer game
import time 

restart = '1'
while restart == '1':

print "Hi there, welcome to Mason's who wants to be a millionaire New Zealand Edition"      # Welcomes user
time.sleep(3)
def main():

    name=raw_input( "Please type in your name then hit Enter")                              # Asks for name
    
    print '\nHello there', name, 'I hope you have fun. Enjoy and good luck!!'               # Prints the stored name with an encouraging message

    print '\nReady'
    time.sleep(1)
    print '\n3'
    time.sleep(1)
    print '\n2'
    time.sleep(1)
    print '\n1'
    time.sleep(1)
    print'\nGo!'
    

    Question1()
    time.sleep(2)
    Question2()
    time.sleep(2)
    Question3()
    time.sleep(2)
    Question4()
    time.sleep(2)
    Question5()
    time.sleep(2)
    Question6()
    time.sleep(2)
    Question7()
    time.sleep(2)
    Question8()
    time.sleep(2)
    Question9()
    time.sleep(2)
    Question10()
start = time.time()
   

    

    


    
def Question1():

    print '\nWhat is the capital of New Zealand?'
    print '\nA) Invercargill'
    print '\nB) Wellington'
    print '\nC) Dunedin'
    print '\nD) Auckland'
    ans1=raw_input ('Answer: ')
    if ans1 == 'A' or ans1=='a':
        print 'Wrong!'
    elif ans1 == 'B'or ans1=='b':
        print 'Correct! Wellington is the Capital of New Zealand, Nice work!'
    elif ans1 == 'C'or ans1== 'c':
        print 'Wrong!'
    elif ans1 == 'D'or ans1== 'd':
        print 'Wrong!'


def Question2():

    print'\nWho is the captain of the AllBlacks Rugby team?'
    print '\nA) Daniel Carter'
    print '\nB) Andrew Hore'
    print '\nC) Conrad Smith'
    print '\nD) Richie McCaw'
    ans2=raw_input ('Answer: ')
    if ans2 == 'A' or ans2=='a':
        print 'Wrong!'
    elif ans2 == 'B'or ans2=='b':
        print 'Wrong!'
    elif ans2 == 'C'or ans2== 'c':
        print 'Wrong!'
    elif ans2 == 'D'or ans2== 'd':
        print 'Correct, Richie McCaw is the AllBlacks captain! Awesome work!'


def Question3():

    print'\nWhat is the weather in dunedin?'
    print '\nA) Miserable'
    print '\nB) Sunny'
    print '\nC) Heat Wave'
    print '\nD) Windy'
    ans3=raw_input ('Answer: ')
    if ans3 == 'A' or ans3=='a':
        print 'Dunedins weather is miserable as always'
    elif ans3 == 'B'or ans3=='b':
        print 'Wrong!'
    elif ans3 == 'C'or ans3== 'c':
        print 'Wrong!'
    elif ans3 == 'D'or ans3== 'd':
        print 'Wrong!'


def Question4():

    print"\nWhat is New Zealand's iconic food?"
    print '\nA) Scones'
    print '\nB) Pizza'
    print '\nC) Pavalova'
    print '\nD) Fish & Chips'
    ans4=raw_input ('Answer: ')
    if ans4 == 'A' or ans4=='a':
        print 'Wrong!'
    elif ans4 == 'B'or ans4=='b':
        print 'Wrong!'
    elif ans4 == 'C'or ans4== 'c':
        print 'Well done! Pavalova is New Zealands iconic food'
    elif ans4 == 'D'or ans4== 'd':
        print 'Wrong!'


def Question5():

    print'\nThe Maori name for the New Zealand is Aoteroa, what does this mean in english?'
    print '\nA) Land of the golden sand'
    print '\nB) Land of the long white cloud'
    print '\nC) Land of the cloudy mountain'
    print '\nD) Land of rain and wind'
    ans5=raw_input ('Answer: ')
    if ans5 == 'A' or ans5=='a':
        print 'Wrong!'
    elif ans5 == 'B'or ans5=='b':
        print 'Correct!Aotearoa means Land of the long white cloud'
    elif ans5 == 'C'or ans5== 'c':
        print 'Wrong!'
    elif ans5 == 'D'or ans5== 'd':
        print 'Wrong!'


def Question6():

    print'\nWellington is the capital of New Zealand, what is the nickname of the parliament building?'
    print '\nA) The Rattery'
    print '\nB) The Ants Nest'
    print '\nC) The Beehive'
    print '\nD) The Rabbits Warren'
    ans6=raw_input ('Answer: ')
    if ans6 == 'A' or ans6=='a':
        print 'Wrong!'
    elif ans6 == 'B'or ans6=='b':
        print 'Wrong!'
    elif ans6 == 'C'or ans6== 'c':
        print 'Correct! The Beehive is the nickname for the New Zealand Parliament'
    elif ans6 == 'D'or ans6== 'd':
        print 'Wrong!'


def Question7():

    print'\nWhat is the highest mountain in New Zealand?'
    print '\nA) Mount Cook'
    print '\nB) Mount Taranaki'
    print '\nC) Mount Dobson'
    print '\nD) Mount Tasman'
    ans7=raw_input ('Answer: ')
    if ans7 == 'A' or ans7=='a':
        print 'Correct! Mount Cook is the highest mountain in New Zealand'
    elif ans7 == 'B'or ans7=='b':
        print 'Wrong!'
    elif ans7 == 'C'or ans7== 'c':
        print 'Wrong!'
    elif ans7 == 'D'or ans7== 'd':
        print 'Wrong!'



def Question8():

    print'\nNew Zealands population is roughly?'
    print '\nA) 6 million'
    print '\nB) 2.8 million'
    print '\nC) 4 million'
    print '\nD) 7.3 milllion'
    ans8=raw_input ('Answer: ')
    if ans8 == 'A' or ans8=='a':
        print 'Wrong!'
    elif ans8 == 'B'or ans8=='b':
        print 'Wrong!'
    elif ans8 == 'C'or ans8== 'c':
        print 'Correct! The population of New Zealand is roughly 4 million people!'
    elif ans8 == 'D'or ans8== 'd':
        print 'Wrong!'

def Question9():

    print"\nWhich New Zealand reptile is the world's most ancient animal?"
    print '\nA)Tuatara'
    print '\nB)Komodo Dragon'
    print '\nC)New Zealand Alligator'
    print '\nD)King Cobra'
    ans9=raw_input ('Answer: ')
    if ans9 == 'A' or ans9=='a':
        print 'Correct! The Tuatara is the most ancient animal and is from New Zealand!'
    elif ans9 == 'B'or ans9=='b':
        print 'Wrong!'
    elif ans9 == 'C'or ans9== 'c':
        print 'Wrong!'
    elif ans9 == 'D'or ans9== 'd':
        print 'Wrong!'

def Question10():

    print'\nName the body of water that separates the North and South Islands'
    print '\nA)Bass Strait'
    print '\nB)Tasman Sea'
    print '\nC)Foveaux Strait'
    print '\nD)Cook Strait'
    ans10=raw_input ('Answer: ')
    if ans10 == 'A' or ans10=='a':
        print 'Wrong!'
    elif ans10 == 'B'or ans10=='b':
        print 'Wrong!'
    elif ans10 == 'C'or ans10== 'c':
        print 'Wrong!'
    elif ans10 == 'D'or ans10== 'd':
        print 'Correct! The body of water that separates the North and South Islands is the Cook Strait!'
        time.sleep(1)
        end = round(start - time.time(), 2)
        print "you completed this quiz in " + str(end) + " seconds."




restart = input("Would you like to play again? Enter 1 for YES and 2 for NO.")


    

    


        



                
main()   

    
    
        
    













It doesnt like the indented blocks
Was This Post Helpful? 0
  • +
  • -

#13 midknight51  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 47
  • Joined: 25-September 12

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.

This post has been edited by midknight51: 05 October 2012 - 12:15 AM

Was This Post Helpful? 0
  • +
  • -

#14 Osbornezo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 12

Re: Restarting a program

Posted 05 October 2012 - 12:14 AM

View Postmidknight51, on 05 October 2012 - 12:03 AM, said:

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.
Could you try the code yourself? It goes super weird on me, it repeats the intro line untill i exit the program
Was This Post Helpful? 0
  • +
  • -

#15 midknight51  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 47
  • Joined: 25-September 12

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.

Remember to watch your Indentation!!!!~!

#Import Statements
import time

#Variables
restart = '1'

#Definitions

#Program
while restart == '1':
    main()
    restart = raw_input('Would you like to play again? Press 1 for YES, 2 for NO: ')

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1