2015-07-20 113 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

相关问题