i28v
i28v

Reputation: 33

How do I create a loop in python

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`

Upvotes: 0

Views: 87

Answers (1)

Travis Cook
Travis Cook

Reputation: 171

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

Upvotes: 2

Related Questions