我的代码突然停止了......?

时间:2016-01-10 19:27:55

标签: python

我正常编写了我的代码,但出于某种原因,当它到达def jscript()之前的时候它就停止了。没有错误或任何它只是停止。有人可以帮我。对不起所有的代码...这是我的所有代码尝试,然后你会明白发生了什么。我认为复制和粘贴搞砸了缩进。

def script():
            Wins = 0
            Fails = 0
            Energy = 20
            Fuel = 40
            Oxygen = 30
            Coins = 10
            Life = 100
            AlienLife = 250
            global Energy
            global Fuel
            global Oxygen
            global Coins
            global Life
            global Wins
            global Fails

            time.sleep(5)
        firstName = input("Hello! What is your first name? ") 
        surnameName = input(" And what is your surname? ")
        fullName = firstName + " " + surnameName
        print ("Ok then. Welcome,",fullName,"to the Age Of Space! Here you will have to drive your spaceship victoriously and survive all enemy threats. Your mission is to free Earth from the alien abduction!")
        time.sleep(7)
        print("Btw good luck in defeating the alien king! ;)")
        time.sleep(3)
        print("Your spaceship will take off in 5...")
        time.sleep(2)
        print("4...")
        time.sleep(1)
        print("3...")
        time.sleep(1)
        print("2...")
        time.sleep(1)
        print("1...")
        time.sleep(2)
        print ("BLAST OFF!!!")
        time.sleep(5)
        print("You are now in the depths of space. Commander please ensure that you keep safe during your journey. Good luck!")
        time.sleep(2)
        sleep = input("Commander, would you like to sleep now? If yes then we can take out your bed. ")
        if sleep == "yes":
            Energy = Energy + 5
            print("We have taken out your bed. The spaceship is on autopilot and you may now sleep.")
            time.sleep(4)
            print("2 weeks later...")
        else:
            Energy = Energy - 5
            print("Ok. It is all your choice. BUT you WILL lose energy. Lets carry on with the journey. You have",Energy,"energy remaining.")
            time.sleep(4)
        print("Commander, you have been extremely successful so far. Well done and keep it up!")
        time.sleep(6)
        direction = input("Oh no Sir! There is trouble ahead! Please make your decision quick. It's a matter of life and death. It is also a matter of chance! There are many asteroids ahead. You may either go forwards, backwards, left or right. Make your decision...before it's too late! ")  
        if direction == "left":
            Coins = Coins + 15
            Fuel =  Fuel - 15
            while True:
                print ("You have managed to pass the asteroids, you may now carry on. You have",Fuel,"fuel left.")
                break
                continue
        elif direction == "backwards":
            print("You have retreated and gone back to Earth. You will have to start your mission all over again.")
            time.sleep(2.5)
            print("The game will now restart.")
            time.sleep(2)
            print("Please wait...\n"*3)
            time.sleep(5)
            keep_playing = True
            while True:
                 script()
        elif direction == "forwards":
            Fails = Fails + 1
            print("You have crashed and passed away. Your bravery will always be remembered even if you did fail terribly. You have failed",Fails,"times.")
            time.sleep(3)
            ans = input("Do you want to play again? ")
            if ans == "yes":
                time.sleep(6)
                script()
            else:
                print("Our Earth is now an alien world...")
        else:
            Fuel =  Fuel - 10
            Coins = Coins + 15
            print("You have landed on a planet and have",Fuel,"fuel left.")
            time.sleep(1) 
            def myscript():
                Fuel = 30
                Oxygen = 30
                Coins = 25
                print("What do you want to buy? You have currently",Coins,"coins and",Fuel,"fuel.")
                print ("You have a choice between the following and may only buy one:")
                time.sleep(1)    
                pistol = 20
                print("A pistol for 20 coins")
                time.sleep(1)
                costfuel = 10
                print("15 fuel for 10 coins")
                time.sleep(1)
                shotgun = 40
                print("A shotgun for 40 coins")
                time.sleep(1)
                costoxygen = 10
                print("10 oxygen for 10 coins")
                time.sleep(1)
                costlife = 50
                print("or 5 life for 50 coins")
                time.sleep(1)
                choose = input("You may now choose which of the following you want - please type it in e.g fuel. ")
                if choose == "pistol":
                    while Coins < pistol:
                        print ("You cannot buy this as you do not have enough money. Please type in something else.")
                        time.sleep(4)
                        myscript()
                        break
                    else:
                        Coins = Coins - 20
                        print ("Thank you for purchasing the pistol! You have",Coins,"coins remaining.")
                elif choose == "fuel":
                    while Coins < costfuel:
                        print ("You cannot buy this as you do not have enough money. Please type in something else.")
                        time.sleep(4)
                        global Coins
                        myscript()
                        break
                    else:
                        Coins = Coins - 10
                        Fuel = Fuel + 15
                        print ("Thank you for purchasing the fuel! You have",Fuel,"fuel remaining and",Coins,"coins remaining.")
                elif choose == "shotgun":
                    while Coins < shotgun:
                        print ("You cannot buy this as you do not have enough money. Please type in something else.")
                        time.sleep(4)
                        global Coins
                        global Fuel
                        Coins = 15
                        Fuel = 45
                        myscript()
                        break
                    else:
                        Coins = Coins - 40
                        print ("Thank you for purchasing the shotgun! You have",Coins,"coins remaining")
                elif choose == "oxygen":
                    while Coins < costoxygen:
                        print ("You cannot buy this as you do not have enough money. Please type in something else.")
                        time.sleep(4)
                        global Coins
                        myscript()
                        break
                    else:
                        Coins = Coins - 10
                        Oxygen = Oxygen + 10 
                        print ("Thank you for purchasing the oxygen! You have",Coins,"coins remaining and",Oxygen,"oxygen remaining.")
                        global Coins
                        Coins = 15
                        global Oxygen
                        Oxygen = 40
                else:   
                    while Coins < costlife:
                        print ("You cannot buy this as you do not have enough money. Please type in something else.")
                        time.sleep(4)
                        myscript()
                        break
                    else:
                        Coins = Coins - 50
                        print ("Thank you for purchasing more life! You have",Coins,"coins remaining and",Life,"life remaining!")
            myscript()
        time.sleep(3.5)
        prin = input("Well done Commander! Bravo! You are doing excellent! Would you like to eat? ")
        if prin == "yes":
            Oxygen = Oxygen - 10
            print ("There is a lot of food in the cupboard please help yourself.")
        else:
            print ("Very well, hope you don't get hungry Sir.")
            time.sleep(2.5)
        papy = input("Would you like to go to sleep commander? ")
        if papy == "yes":
            Energy = Energy + 5
            print("Hope you have a great nap and fill yourself with enrgy Sir! Your bed has been taken out and you may now sleep.")
            time.sleep(6)
            print("15 hours later...")
            time.sleep(3.5)
            print("Sir! Sorry to interfere your sleep but you have landed on a planet!")
            def minescript():
                global Coins
                global Oxygen
                global Fuel
                print("What do you want to buy? You have currently",Coins,"coins and",Fuel,"fuel.")
                print ("You have a choice between the following and may only buy one:")
                time.sleep(1)    
                pistol = 20
                print("A pistol for 20 coins")
                time.sleep(1)
                costfuel = 10
                print("15 fuel for 10 coins")
                time.sleep(1)
                shotgun = 40
                print("A shotgun for 40 coins")
                time.sleep(1)
                costoxygen = 10
                print("10 oxygen for 10 coins")
                time.sleep(1)
                choose = input("You may now choose which of the following you want - please type it in e.g fuel. If you do not have enough money to buy anything type in 'nothing'. ")
                if choose == "pistol":
                    while Coins < pistol:
                        print ("You cannot buy this as you do not have enough money. Please type in something else.")
                        time.sleep(4)
                        minescript()
                        break
                    else:
                        Coins = Coins - 20
                        print ("Thank you for purchasing the pistol! You have",Coins,"coins remaining.")
                elif choose == "fuel":
                    while Coins < costfuel:
                        print ("You cannot buy this as you do not have enough money. Please type in something else.")
                        time.sleep(4)
                        myscript()
                        break
                    else:
                        Coins = Coins - 10
                        Fuel = Fuel + 15
                        print ("Thank you for purchasing the fuel! You have",Fuel,"fuel remaining and",Coins,"coins remaining.")
                elif choose == "shotgun":
                    while Coins < shotgun:
                        print ("You cannot buy this as you do not have enough money. Please type in something else.")
                        time.sleep(4)
                        minescript()
                        break
                    else:
                        Coins = Coins - 40
                        print ("Thank you for purchasing the shotgun! You have",Coins,"coins remaining")
                elif choose == "oxygen":
                    while Coins < costoxygen:
                        print ("You cannot buy this as you do not have enough money. Please type in something else.")
                        time.sleep(4)
                        minescript()
                        break
                    else:
                        Coins = Coins - 10
                        Oxygen = Oxygen + 10 
                        print ("Thank you for purchasing the oxygen! You have",Coins,"coins remaining and",Oxygen,"oxygen remaining.")
                else:        
                        print ("Hope you find something to buy next time :)")
            minescript()    
        else:
            Energy = Energy - 10
            print("Very well Sir. You have",Energy,"energy remaining now.")
        time.sleep(4)
        print("Now you will see a map of where you are and how close you are to the alien palace.")
        time.sleep(2)
        print("Please wait...")
        time.sleep(2)
        print("Please wait...")
        time.sleep(2)
        print("Please wait...")
        time.sleep(8)
        print("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
        print("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
        print("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
        print("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
        print("ZZZZZZZZZZZZZZZZZZZZZ00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
        print("ZZZZZZZZZZZZZZZZZZZZZ00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
        print("                   ZZ00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
        print("                   ZZ00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
        print("                   ZZ00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
        print("                   ZZ00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
        print("                   ZZ00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
        print("                   ZZ000000000000000000MMMM000000000000000000000000PPPPPPPPPP0000000000000000000000000000000000000000000000-00000000-0000000000000000000")
        print("                   ZZ000000000000000000MMMM000000000000000000000000P        P000000000000000000000000000000000000000000000- -000000- -000000000000000000")
        print("                   ZZ000000000000000000MMMM00000000MMM0000000000000P PLANET P00000000000000000000000000000000000000000000-   -0000-   -00000000000000000")
        print("      EARTH        ZZ0000000000000000000---------00MMM0000000000000P        P000000000000000000000000000000000*0000000000-    -000-   -00000 **     ** 0")
        print("(where you started)ZZ--------------------000MMM0--------------00000PPPPPPPPPP0000000000000000000000000000000*****00000000-  a -000-   -00000 ..     .. 0")
        print("                   ZZ00000000000000000000000MMM00000000000000-0000000000000000000000000IIIII0000000000000000*****00000000-  l -000-   -00000     oo    0")
        print("                   ZZ000000000000000000000000000000MMM0000000--------------------------I U I00000000000000000000000000000-  i -000-   -00000           0")
        print("                   ZZ000000000000000000000MMMMM0000000000000000000000000000000000000000IIIII00000000000000000000000000000-  e -000-   -00000 (_______) 0")
        print("                   ZZ000000000000000000000MMMMM00000000000000000000000000000000000000000000000000000000000000000000000000-  n -000-   -00000           0")
        print("                   ZZ000000000000000000000MMMMM0000000000000000000000000000000000000000000000000000000000000000000*000000-    -000- p -00000000000000000")
        print("                   ZZ0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*****0000-    -000- a -00000000000000000")
        print("                   ZZ00000000000000000000000000000000000PPPPPPPPPP0000000000000000000000000000000000000000000000*****0000-    -000- l -00000000000000000")
        print("ZZZZZZZZZZZZZZZZZZZZZ00000000000000000000000000000000000P        P0000000000000000000000000000000000000000000000000000000-    -000- a -00000000000000000")
        print("ZZZZZZZZZZZZZZZZZZZZZ00000000000000000000000000000000000P PLANET P0000000000000000000000000000000000000000000000000000000-    -000- c -00000000000000000")
        print("00000000000000000000000000000000000000000000000000000000P        P0000000000000000000000000000000000000000000000000000000-    -000- e -00000000000000000")   
        print("00000000000000000000000000000000000000000000000000000000P        P00000000000000000000000000000000000000000000000000000000-  -00000- -000000000000000000")
        print("00000000000000000000000000000000000000000000000000000000PPPPPPPPPP000000000000000000000000000000000000000000000000000000000--0000000-0000000000000000000")
        print("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
        print("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
        print("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
        print("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
        print("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
        print("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
        print("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
        time.sleep(5)
        print("MMM = asteroids")
        time.sleep(1.5)
        print("U = where you are")
        time.sleep(1.5)
        print("---- = path")
        time.sleep(1.5)
        print("***** = alien security/alien guards ")
        time.sleep(6)
        print("Please ensure that you have reviewed the map - thank you.")
        print("Commander, you have progressed extremely well and will be well remembered in history. For your progress you have been awarded 100 extra coins! Please use it wisely if you get a chance. Remember you have",Life,"lives and the alien king has",AlienLife,"lives!")
        global Coins
        Coins = Coins + 100
        print("You now have",Coins,"coins.")
        p = input("You have " + str(Energy) + " energy left commander, would you like to go to sleep? ")
        if p == "yes":  
            print("Hope you have a great sleep THIS time :)")
            Energy = Energy + 2
        else:
            print("Very well.")
            Energy = Energy - 5
        if Energy == 0:
            print("You have run out of energy master! You must retreat now!")
            time.sleep(1)
            print ("Your mission is over. You have travelled back to Earth on autopilot. You were too tired to carry on.")
            time.sleep(2)
            print ("The game will now restart.")
            time.sleep(4)
            script()
        else:
            Fuel = Fuel - 5
            print("You have",Energy,"energy and",Fuel,"fuel left.")
            print("You are doing very well. Soon you will have to battle the alien guards so be prepared to loose some lives :). No pressure.")
            time.sleep(3)
            print("After an hour...")
            time.sleep(3)
            Fuel = Fuel - 5  
            if Energy == 0:
                print("You have run out of energy master! You must retreat now!")
                time.sleep(1)
                print ("Your mission is over. You have travelled back to Earth on autopilot. You were too tired to carry on.")
                time.sleep(2)
                print ("The game will now restart.")
                time.sleep(4)
                script()
            else:
                print("Sir! The ship has broken down. You have to go outside to fix it. As a consequence you will loose quite a bit of oxygen. You have",Oxygen,"oxygen left and",Energy,"energy left. Please make a choice between the following:")
                time.sleep(2)
                print("Repair fully - result in 10 oxygen loss and 10 energy loss.")
                time.sleep(2)
                print("Repair half - result in 5 oxygen loss and 5 energy loss. Then you will be able to finish it off the next day after sleeping.")
                time.sleep(2)
                print("Do not repair now - result in no loss of energy and oxygen but you will not be able to finish the repair the next day either.")
        def jscript():
            global Coins
            global Oxygen
            global Fuel
            repair = input("Please type in your choice. E.g. full or half or not now. ")
            if repair == "full":
                while Energy < 10:
                    print("You cannot carry out this option, sorry.")
                    time.sleep(3)
                    jscript()
                    break
                else:
                    Oxygen = Oxygen - 10
                    Energy = Energy - 10
                    print("The repair has been successful. You now have",Fuel,"fuel",Oxygen,"oxygen and",Coins,"remaining!")
            elif repair == "half":
                while Energy < 5:
                    print("You cannot carry out this option, sorry.")
                    time.sleep(3)
                    jscript()
                    break
                else:
                    Oxygen = Oxygen - 5
                    Energy = Energy - 5
                    print("The half repair has been successful. You now have",Fuel,"fuel",Oxygen,"oxygen and",Coins,"remaining!")
                    time.sleep(2)
                    print("Commander hopefully this will not seem too aggressive to you, but I think you HAVE to sleep now. The beds are ready so take a rest...")
                    Energy = Energy + 6
                    time.sleep(3)
                    print("The next day...")
                    time.sleep(3)
                    print("Congarats! The rest of the spaceship has been repaired successfully without any loss! ;)")
            else:
                print("There will be no repair done.")
                time.sleep(2.5)
                print("The next day...")
                time.sleep(3.5)
                foo = ['a'] * 65 + ['b'] * 35
                an = random.choice(foo)
                if an == "a":
                    print("hi")
                else:
                    print("SHing CHing")
    script()

2 个答案:

答案 0 :(得分:1)

我会假设这是你想要得到的。你不应该在if-else statement中定义一个函数。你应该打电话给它。

else:
    print("Sir! The ship has broken down. You have to go outside to fix it. As a consequence you will loose quite a bit of oxygen. You have",Oxygen,"oxygen left and",Energy,"energy left. Please make a choice between the following:")
    time.sleep(2)
    print("Repair fully - result in 10 oxygen loss and 10 energy loss.")
    time.sleep(2)
    print("Repair half - result in 5 oxygen loss and 5 energy loss. Then you will be able to finish it off the next day after sleeping.")
    time.sleep(2)
    print("Do not repair now - result in no loss of energy and oxygen but you will not be able to finish the repair the next day either.")* This is where the code stops
    jscript()

def jscript():
    global Coins
    global Oxygen
    global Fuel
    repair = input("Please type in your choice. E.g. full or half or not now. ")
    if repair == "full":
        while Energy < 10:
            print("You cannot carry out this option, sorry.")
            time.sleep(3)
            jscript()
            break
    else:
        Oxygen = Oxygen - 10
        Energy = Energy - 10
        print("The repair has been successful. You now have",Fuel,"fuel",Oxygen,"oxygen and",Coins,"remaining!")

另外,为什么你在内部调用函数然后打破它是没有意义的,然后你做一会儿看完就完成了一次。删除所有它没有意义,只是使用另一个if

def jscript():
    global Coins
    global Oxygen
    global Fuel
    repair = input("Please type in your choice. E.g. full or half or not now. ")
    if repair == "full":
        if Energy < 10:
            print("You cannot carry out this option, sorry.")
            time.sleep(3)
        else:
            break
    else:
        Oxygen = Oxygen - 10
        Energy = Energy - 10
        print("The repair has been successful. You now have",Fuel,"fuel",Oxygen,"oxygen and",Coins,"remaining!")

答案 1 :(得分:1)

您的代码中存在几个问题,因此很难遵循它的逻辑和结构。所以这里有一些评论如何将代码重构为更有用的东西,同时仍保持相同类型的游戏逻辑。

  • 引入打印和睡眠功能 - 在顶层引入功能以打印消息并暂停一段时间会大大减少您的代码。
  • 使用"Welcome {} to the Age Of Space".format(fullname)简化字符串处理 - 这与之前的建议相结合可能会产生以下功能:

    def output_message(message, sleep_time):
        """Outputs a message, followed by a sleep period."""
    
        print(message)
        time.sleep(sleep_time)
    

    然后可以使用:

    output_message("Welcome {}, to the Age Of Space!".format(fullName), 7)
    output_message("Good luck in defeating the alien king! ;)", 3)
    output_message("Your spaceship will take off in 5...", 2)
    output_message("4...", 1)
    output_message("3...", 1)
    output_message("2...", 1)
    
  • 在文件开头收集函数 - 尽管在Python中几乎任何级别定义函数都是合法的,但强烈建议在文件的开头定义所有函数。换句话说,如下所示:

    import time
    
    def output_message(message, sleep_time):
       """My docstring."""
       ... code as before ...
    
    
    def script():
        ...
        code of script()
        ...
    
    def myscript():
        ... code of myscript ...
    
    def main():
        while True:
            start_mission()
            # Ask if user wants to restart game
            restart = input("Do you want to restart game? ")
    
            if restart == "no":
                break
    
    
    if __name__ == "__main__":
        main()
    

    正如您所看到的,所有函数都从行首开始,这是正常的,只要您没有定义类方法,这些方法将进一步缩进。

    PS!堆栈交换提示:将文本粘贴到Stack Exchange网站的编辑框后,标记所有内容并点击 Ctrl-K 正确缩进代码。

  • 避免顺序递归函数调用 - 这不是从script()内调用def script(),而是应该允许脚本结束,然后再调用它如果用户选择重新启动。该结构在先前的代码块中描述。

    类似的结构应该适用于调用minescript()myscript()jscript()。所有这些都是名不详细的,并没有描述实际发生的事情。似乎minescript()myscript()非常相似,应该可以称为buy_stuff()

  • 错误:您不会在jscript()之外调用jscript(),并且错误的缩进级别 - 所有其他内部函数你有,minescript()myscript遵循这种模式:

    if something:
       ...
    elif something else:
       ...
    else
       ...
       def aFunction():
           ...
       aFunction()
    if yet_another_condition:
       ...
    

    但对于jscript(),您已将缩进移出两个级别,并且在定义之后调用jscript()

  • 购买东西时避免奇怪的循环 - 你有一个与内部函数相关的奇怪循环概念,比如minescript()的简化版本:

   def minescript():
       global Coins
       global Oxygen
       global Fuel
       print("What do you want to buy? You have currently",Coins,"coins and",Fuel,"fuel.")
       output_message("You have a choice between the following and  may only buy one:", 1)
       pistol = 20
       output_message("A pistol for 20 coins", 1)
       costfuel = 10
       print("15 fuel for 10 coins")
       choose = input("You may now choose which of the following you want - please type it in e.g fuel. If you do not have enough money to buy anything type in 'nothing'. ")
       if choose == "pistol":
           while Coins < pistol:
               output_message("You cannot buy this as you do not have enough money. Please type in something else.", 4)
               minescript()
               break
           else:
               Coins = Coins - 20
               print("Thank you for purchasing the pistol! You have",Coins,"coins remaining.")
       elif choose == "fuel":
           while Coins < costfuel:
               print ("You cannot buy this as you do not have enough money. Please type in something else.")
               time.sleep(4)
               myscript()
               break
           else:
               Coins = Coins - 10
               Fuel = Fuel + 15
               print ("Thank you for purchasing the fuel! You have",Fuel,"fuel remaining and",Coins,"coins remaining.")
       else:        
               print ("Hope you find something to buy next time :)")
   minescript() 

这是错误的代码,因为它以递归方式调用它,而不是进行内部循环,直到用户购买了用户可以买得起的东西或者选择什么都不买。购买东西的更好的代码结构是:

def minescript():
    global Coins
    global Oxygen
    global Fuel
    print("What do you want to buy? You have currently",Coins,"coins and",Fuel,"fuel.")
    output_message("You have a choice between the following and may only buy one:", 1)
    pistol = 20
    output_message("A pistol for 20 coins", 1)
    costfuel = 10
    print("15 fuel for 10 coins")
    while True:
        choose = input("You may now choose which of the following you want - please type it in e.g fuel. If you do not have enough money to buy anything type in 'nothing'. ")
        if choose == "pistol":
            if Coins < pistol:
                output_message("You cannot buy this as you do not have enough money. Please type in something else.", 4)
            else:
                Coins = Coins - pistol
                print("Thank you for purchasing the pistol! You have",Coins,"coins remaining.")
                break
        elif choose == "fuel":
            if Coins < costfuel:
                output_message("You cannot buy this as you do not have enough money. Please type in something else.", 4)
        else:
            Coins = Coins - costfuel
            Fuel = Fuel + 15
            print ("Thank you for purchasing the fuel! You have",Fuel,"fuel remaining and",Coins,"coins remaining.")
            break
    else:        
            print ("Hope you find something to buy next time :)")
            break
minescript() 

更好的版本是将这些东西保留在相应价格的列表中,并使用它来进一步简化功能。

def buy_stuff(items):
    global Coins
    global Oxygen
    global Fuel
    print("What do you want to buy? You have {} Coins, and {} fuel".format(Coins, Fuel))
    output_message("You have a choice between the following and may only buy one:", 1)
    for item in items:
        print("    {} : {} coins".format(item, items[item]))

    while True:
        choice = input("Type item you want to purchase or 'nothing'")
        if choice in items:
            if items[choice] > Coins:
                output_message("You don't have enough money. Try something else.", 4)
            else:
                Coins = Coins - items[choice]
                print("You bought: {}, and have {} coins left.".format(choice, Coins))
                break
        elif choose == "nothing":
            print ("Hope you find something to buy next time :)")                    break
        else:        
            print("Couldn't find that item. Use 'nothing' if you don't want to buy anything.")

 store_items = {
     'pistol': 20,
     'fuel': 15,
     'energy': 20
 }
 buy_stuff(store_items)

Code is untested, but you should get the gist of it. And it can be adapted to changing the different variables by utilizing tuples or similar.

希望这些提示可以帮助您解决问题,并大大简化您的代码。最后,你还应该考虑避免使用全局变量,并可能简化一些消息,或者考虑预定义一些较大的文本部分,以进一步增强对代码中游戏逻辑的关注,而不是所有的“嘈杂”。目前的短信。