Ex 36 Game Functions

Good evening,

I am completing Ex 36 and I have a question about best practice. I have a death function that accomplishes what I want it to (I think), but I wrote it in two different ways. Which follows best practice, and why? Again, both functions run.

Lastly, is there a better way to post this to make it easier for other readers of the forum? Should I post in github and send the link? Is that acceptable?

Example 1:

#This is an attempt at a text-based survival game game.  I'm really not sure how it is going to work out.
def start():
    print("""Your airplane was struck by a missle and you have crash-landed on a mountain side.  Your communications were destroyed.  Everyone onboard has perished except for you and your 11 year-old son.  After looking through the rubble, you were able to secure enough food to last three days at the most.  You have no means to alert anyone of your whereabouts.  How will you survive?  Will your son make it, or will you leave him fend for himself?\n\nAbout 150 yards ahead, you see something unusual.  As you and your son slowly stagger towards the site, you determine that it is a cave.  What will you do?  Will you go in or stay outside?""")
	
	choice = input("Do you want to go in or stay outside?  > ")
	if choice == "go in":
		print("The ground tremors and rocks seal you in separating you from your son.  While he can barely hear you, you tell him to hang tight and try to find help.")
	elif choice == "stay outside":
		death("How are you going to survive if you do nothing?  You lose!")
	else:
		death("Don't play yourself!  Your choices are to go in or stay outside.  Bye LOSER!")                  	
	
    def death(how):
    	print(how)
    	print("Would you like to try again?  Enter 1 for Yes or 2 for No.")
	
	#try:
	choice = input("> ")
	#	choice = int(input("> "))
		
	#except:
	#	print("Enter 1 for Yes or 2 for No.")	
	print("Enter 1 for Yes or 2 for No.")
		
	#print(">>>", choice)
	
	#if choice == 1:  #I tried try: and except: but it didn't work.
	#	start()
	#elif choice == 2:
	#	print("Thanks for playing.  Good-bye!")	
	#print(">>>>>>>>", repr(choice))
	
	if choice in "1":  #I tried try: and except: but it didn't work.
		start()
	elif choice in "2":
		print("Thanks for playing.  Good-bye!")
	#elif choice.isdigit() != 1 or 2:
		#print("Not an option. ")
	else:
		print("You obviously do not want to play.  Ciao!")
    	
    		

    start()

Example 2:

#This is an attempt at a text-based survival game game.  I'm really not sure how it is going to work     out.

def start():
	print("""Your airplane was struck by a missle and you have crash-landed on a mountain side.  Your communications were destroyed.  Everyone onboard has perished except for you and your 11 year-old son.  After looking through the rubble, you were able to secure enough food to last three days at the most.  You have no means to alert anyone of your whereabouts.  How will you survive?  Will your son make it, or will you leave him fend for himself?\n\nAbout 150 yards ahead, you see something unusual.  As you and your son slowly stagger towards the site, you determine that it is a cave.  What will you do?  Will you go in or stay outside?""")

choice = input("Do you want to go in or stay outside?  > ")
if choice == "go in":
	print("The ground tremors and rocks seal you in separating you from your son.  While he can barely hear you, you tell him to hang tight and try to find help.")
elif choice == "stay outside":
	death("How are you going to survive if you do nothing?  You lose!")
else:
	death("Don't play yourself!  Your choices are to go in or stay outside.  Bye LOSER!")                  	

def death(how):
	print(how)
	print("Would you like to try again?  Enter 1 for Yes or 2 for No.")

while True:
	try:
		choice = int(input("> "))
		if choice == 1:
			start()
    		elif choice == 2:
				print("Thanks for playing.  Good-bye.")
			break
		except ValueError:
			print("Invalid selection!!  Enter 1 for Yes or 2 for No.")

start()

Yes, if you have github that’s always a good option. I prefer the try/except method personally. Also, your indentation on this is crazy. Are you using tabs or spaces? You should definitely switch to spaces only. Check your editor settings.

A free service run by Zed A. Shaw for learncodethehardway.org.