2015-07-20 73 views
-2

我真的需要知道爲什麼當我輸入這段代碼時,python沒有出現。當我在python中輸入時,什麼都沒有出現

這是因爲def myscript()

我是新來的Python和本網站 - Stackoverflow。

import time 
def myscript(): 
    print("What do you want to buy? You have currently") 
    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 pistol. ") 
    if choose == "pistol": 
     while Coins < pistol: 
      print ("You cannot buy this as you do not have enough money.") 
      time.sleep(4) 
      myscript() 
     else: 
      print ("Thank you for purchasing the pistol! You have",Coins,"coins remaining") 
    elif choose == "costfuel": 
     while Coins < costfuel: 
      print ("You cannot buy this as you do not have enough money.") 
      time.sleep(4) 
      myscript() 
     else: 
      print ("Thank you for purchasing the fuel! You have",Fuel,"fuel remaining and",Coins,"coins remaining.") 
    else: 
     while Coins < shotgun: 
      print ("You cannot buy this as you do not have enough money.") 
      time.sleep(4) 
      myscript() 
     else: 
      print ("Thank you for purchasing the shotgun! You have",Coins,"coins remaining") 
    myscript() 
+3

de-indint最後一行四個空格/ – NightShadeQueen

+1

您確定底部的myscript()標籤是正確的。我認爲這是你的函數定義的一部分。 – marsh

+0

您示例中的縮進不正常。請糾正它。執行代碼時是否收到任何錯誤消息?變量「硬幣」從哪裏來?我不知道你在哪裏宣佈它。 – cezar

回答

4

myscript()是製表符錯誤的,它是你函數定義的一部分。導入時間也看起來不對。

這裏是更正後的代碼。你看得到差別嗎?

import time  
def myscript(): 
    print("What do you want to buy? You have currently") 
    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 pistol. ") 
    if choose == "pistol": 
     while Coins < pistol: 
      print ("You cannot buy this as you do not have enough money.") 
      time.sleep(4) 
      myscript() 
     else: 
      print ("Thank you for purchasing the pistol! You have",Coins,"coins remaining") 
    elif choose == "costfuel": 
     while Coins < costfuel: 
      print ("You cannot buy this as you do not have enough money.") 
      time.sleep(4) 
      myscript() 
     else: 
      print ("Thank you for purchasing the fuel! You have",Fuel,"fuel remaining and",Coins,"coins remaining.") 
    else: 
     while Coins < shotgun: 
      print ("You cannot buy this as you do not have enough money.") 
      time.sleep(4) 
      myscript() 
     else: 
      print ("Thank you for purchasing the shotgun! You have",Coins,"coins remaining") 
myscript() 
+0

我不知道你的意思 – wacraby

+0

然後你應該看看[官方Python教程](https://docs.python.org/3.4/tutorial)。 – TigerhawkT3

+0

Python使用縮進,myscript()調用您的代碼所在的函數。但它被縮進以便它成爲函數的一部分。因此它永遠不會被召喚。另外如果你的函數被調用,它會遞歸地調用它自己。 – marsh

1

代替:

import time  
    def myscript(): 
     print("What do you want to buy? You have currently") 
     print ("You have a choice between the following and may only buy one:") 
     time.sleep(1) 
     ... 

寫:

import time  
def myscript(): 
    print("What do you want to buy? You have currently") 
    print ("You have a choice between the following and may only buy one:") 
    time.sleep(1) 
    ... 

然後調用它

myscript() 

你面對一個壓痕問題。

+0

哦,我已經試過,但由於某種原因,它說myscript()沒有定義 – wacraby

相關問題