0

just like in my other question, this one is about that mining game i'm working on, there is a bug, when I run it it does this: when I test it

It closes the program instead of doing it over and over again, I want to create a loop, but I don't know how to create the type of loop for this type of problem. Help please?

here is the code: `

import random
start = raw_input("Welcome to the gold mine game! type 'start' to start playing ")
if str.upper(start) == ("START"):
  default_cash = 14
  print "you have %s cash" % default_cash
  choices = raw_input("What would you like to do? type 'dig' to dig for ores or type 'shop' to buy equitment ")
  if str.upper(choices) == ("DIG"):
    ores = ["nothing", "nothing", "nothing", "nothing", "nothing", "nothing", "nothing", "nothing", "nothing", "nothing", "nothing", "iron", "iron", "iron", "iron", "silver", "silver", "gold"]
    ores_random = random.choice(ores)
    print "you found %s!" % ores_random`
1
  • Do a quick search for sentinel loops. They are basically while loops with a variable that is either true or false, at the end of the loop you basically ask the user if they would like to do the loop again and give them the option of changing the variable in order to break out of the loop. edit: look at travis' code, it's what he is doing. Commented Mar 17, 2016 at 23:55

1 Answer 1

2

You could add a while loop to keep program going until given a signal to end. Like:

 import random
 import sys      #to exit program at end by calling sys.exit function

 start = raw_input("Welcome to the gold mine game! type 'start' to start playing ")
 if str.upper(start) == ("START"):
   default_cash = 14
   print "you have %s cash" % default_cash
   choices = raw_input("What would you like to do? type 'dig' to dig for ores, type 'shop' to buy equitment, or type 'quit' to exit the game ") #gave extra instruction to user
   quit_signal = False  #added quit signal and initialized to false
   while not quit_signal:   #repeat until quit signal is true
      if str.upper(choices) == ("DIG"):
         ores = ["nothing", "nothing", "nothing", "nothing", "nothing", "nothing", "nothing", "nothing", "nothing", "nothing", "nothing", "iron", "iron", "iron", "iron", "silver", "silver", "gold"]
         ores_random = random.choice(ores)
         print "you found %s!" % ores_random
         choices = raw_input("What would you like to do? type 'dig' to dig for ores, type 'shop' to buy equitment, or type 'quit' to exit the game ")
      elif str.upper(choices) == ("SHOP"):
         #do something
         choices = raw_input("What would you like to do? type 'dig' to dig for ores, type 'shop' to buy equitment, or type 'quit' to exit the game ")
      elif str.upper(choices) == ("QUIT"):
        quit_signal = True  #set quit signal to true which will end while loop
   sys.exit(0)     #to show successful program termination

Edited: Sorry. Forgot to reask for choice input. Inserted input request to assign choices within while loop after necessary conditions

Sign up to request clarification or add additional context in comments.

2 Comments

when I try that it just says 'you found %s!' over and over again forever and I want i t to say '"What would you like to do? type 'dig' to dig for ores, type 'shop' to buy equitment, or type 'quit' to exit the game "' and do the entire game loop over again.
Edited, put comment at bottom of answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.