当我输入这段代码时,我真的需要知道为什么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()
答案 0 :(得分: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()
答案 1 :(得分: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()
您正面临缩进问题。