import time
import random
inventory = [""]
gold = 0
fish1 = "Mackarel"
fish2 = "Cod"
fish3 = "Salmon"
fish4 = "Herring"
fish5 = "Tuna"
trash1 = "Old Shoe"
trash2 = "Plastic Bag"
trash3 = "Rusted Empty Box"
trash4 = "Plank Fragment"
special1 = "Ring"
fish1_range = range(1,3)
fish2_range = range(3,5)
fish3_range = range(5,7)
fish4_range = range(7,9)
fish5_range = range(9,11)
trash1_range = range(11,16)
trash2_range = range(16,21)
trash3_range = range(21,26)
trash4_range = range(26,31)
special1_range = range(31,32)
print "~~~~WELCOME TO FISHING~~~~"
time.sleep(2)
print "Loading Version 0.2 ..."
time.sleep(2)
print "In this current version the last item in your inventory is sold."
print "To execute another function, for example fish twice you have to wait 20 seconds"
print "This means at the start it will take 20 seconds to load aswell."
def action_function(): #defining action start
action_function()
chance = random.randrange(1,31)
action = raw_input("Do you want to .sell or .fish?")
if action == "sell":
if inventory.index(1) == fish1:
inventory.pop(1)
gold + 5
print "You have sold a Mackarel for 5 gold coins!"
action_function()
if inventory.index(1) == fish2:
inventory.pop(1)
gold + 5
print "You have sold a Cod for 5 gold coins!"
action_function()
if inventory.index(1) == fish3:
inventory.pop(1)
gold + 5
print "You have sold a Salmon for 5 gold coins!"
action_function()
if inventory.index(1) == fish4:
inventory.pop(1)
gold + 5
print "You have sold a Herring for 5 gold coins!"
action_function()
if inventory.index(1) == fish5:
inventory.pop(1)
gold + 5
print "You have sold a Tuna for 5 gold coins!"
action_function()
if inventory.index(1) == trash1:
inventory.pop(1)
gold + 1
print "You have recycled an Old Shoe for 1 gold coin."
action_function()
if inventory.index(1) == trash2:
inventory.pop(1)
gold + 1
print "You have recycled an Plastic Bag for 1 gold coin."
action_function()
if inventory.index(1) == trash3:
inventory.pop(1)
gold + 1
print "You have recycled an Rusted Empty Box for 1 gold coin."
action_function()
if inventory.index(1) == trash4:
inventory.pop(1)
gold + 1
print "You have recycled an Old Shoe for 1 gold coin."
action_function()
if inventory.index(1) == special1:
inventory.pop(1)
gold + 10
print "A rare find, 10 gold pieces will serve you!"
action_function()
if action == "fish":
if random.randrange == fish1_range:
inventory.append(fish1)
print "You have reeled in a Mackarel!"
action_function()
if random.randrange == fish2_range:
inventory.append(fish2)
print "You have reeled in a Cod!"
action_function()
if random.randrange == fish3_range:
inventory.append(fish3)
print "You have reeled in a Salmon!"
action_function()
if random.randrange == fish4_range:
inventory.append(fish4)
print "You have reeled in a Herring!"
action_function()
if random.randrange == fish5_range:
inventory.append(fish5)
print "You have reeled in a Tuna!"
action_function()
if random.randrange == trash1_range:
inventory.append(trash1)
print "You have reeled in a...Shoe..."
action_function()
if random.randrange == trash2_range:
inventory.append(trash2)
print "You have reeled in a...Plastic Bag..."
action_function()
if random.randrange == trash3_range:
inventory.append(trash3)
print "You have reeled in a...Rusted Empty Box..."
action_function()
if random.randrange == trash4_range:
inventory.append(trash4)
print "You have reeled in a...Plank Fragment..."
action_function()
if random.randrange == special1_range:
inventory.append(special1)
print "You find a slightly dirty ring, after clearing the dirt it appears quite nice."
action_function()
if action == "inventory":
print inventory
action_function()
所以我现在已经尝试了数百次,我尝试使用循环,还有很多其他的东西,似乎没有什么工作我想要它。大多数时候,在我定义一个函数的第一部分之后它只是空白,它可能已经走得更远,但我无法分辨。另外你可以从这个尴尬的问题中看出我是编程的新手,请帮助我,如果有人可以解释为什么它只打印文本到动作函数的定义。
答案 0 :(得分:1)
您想在定义后调用该函数:
action_function()
请记住,Python通过缩进来定义块,因此将action_function()
行放在与def action_function():
行相同的缩进级别上。
您还想从功能的第一行删除action_function()
来电。
其他一些提示:
不要创建name_x
系列变量。改为使用列表:
fish = ["Mackarel", "Cod", "Salmon", "Herring", "Tuna"]
如果要在gold
变量中添加5,则需要将其存储在该变量中:
gold = gold + 5
或者更短:
gold += 5
.index(1)
函数查找值1
的列表索引;你可能想直接使用索引:
if inventory[0] == something:
请记住,Python列表索引从 0 开始,而不是1。
如果您对fish和trash变量使用列表,则可以执行以下操作:
if inventory[0] in fish:
sold = inventory.pop(0)
gold += 5
print "You have sold a", sold, "for 5 gold coins!"
并消除所有额外的if
分支。
使用循环代替使用递归:
while True:
# print, get input, do actions
# when done, use `break` to stop the loop.