Python 2.7 如何在python中创建循环

Python 2.7 如何在python中创建循环,python-2.7,loops,Python 2.7,Loops,就像我的另一个问题一样,这是关于我正在开发的采矿游戏,有一个bug,当我运行它时,它会这样做: 它关闭程序,而不是一次又一次地执行,我想创建一个循环,但我不知道如何为这类问题创建循环类型。请帮忙 代码如下:` import random start = raw_input("Welcome to the gold mine game! type 'start' to start playing ") if str.upper(start) == ("START"): default_cash

就像我的另一个问题一样,这是关于我正在开发的采矿游戏,有一个bug,当我运行它时,它会这样做:

它关闭程序,而不是一次又一次地执行,我想创建一个循环,但我不知道如何为这类问题创建循环类型。请帮忙

代码如下:`

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`

您可以添加一个while循环来保持程序运行,直到给出结束信号为止。比如:

 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

编辑:对不起。忘记重新请求选项输入。插入输入请求以在必要条件后在while循环中分配选项

您可以添加while循环以保持程序运行,直到给出结束信号。比如:

 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

编辑:对不起。忘记重新请求选项输入。插入输入请求以在必要条件后在while循环中分配选项

快速搜索sentinel循环。它们基本上是while循环,变量为true或false,在循环结束时,您基本上会询问用户是否愿意再次执行循环,并为他们提供更改变量的选项以打破循环。编辑:看看特拉维斯的代码,这就是他正在做的。快速搜索哨兵循环。它们基本上是while循环,变量为true或false,在循环结束时,您基本上会询问用户是否愿意再次执行循环,并为他们提供更改变量的选项以打破循环。编辑:看travis的代码,这就是他正在做的。当我尝试时,它只会说“你找到了%s!”一次又一次地重复,我想说“你想做什么?键入“dig”来挖掘矿石,键入“shop”来购买设备,或者键入“quit”退出游戏”,然后重复整个游戏循环。编辑后,在回答的底部添加注释,当我尝试时,它只会说“你找到了%s!”一次又一次的重复,我想说“你想做什么?输入‘dig’来挖掘矿石,输入‘shop’来购买设备,或者输入‘quit’退出游戏”,然后重复整个游戏循环。编辑后,在答案底部添加注释