0
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()

所以我现在已经尝试了数百次,我尝试使用循环和大量其他东西,但似乎没有任何东西可以按照我的意愿工作。大多数时候,当我定义一个函数时,它只是在第一部分之后变成空白,它可能会走得更远,但我不知道。另外,正如您从这个尴尬的问题中可以看出的那样,我是编程新手,请帮助我,如果有人能解释为什么它只打印到动作函数定义的文本。

4

1 回答 1

1

您想在定义后调用该函数:

action_function()

请记住,Python 通过缩进定义块,因此将action_function()行放在与行相同的缩进级别def action_function():

您还想action_function()从函数的第一行删除调用。

其他一些提示:

  • 不要创建name_x一系列变量。改用列表:

    fish = ["Mackarel", "Cod", "Salmon", "Herring", "Tuna"]
    
  • 如果要将 5 添加到gold变量中,则需要将其存储回该变量中:

    gold = gold + 5
    

    或者,更短:

    gold += 5
    
  • .index(1)函数查找值的列表索引1;您可能想直接使用索引:

    if inventory[0] == something:
    

    请记住,Python 列表索引从0开始,而不是 1。

    如果您使用列表作为鱼和垃圾变量,您可以执行以下操作:

    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.
    
于 2013-06-07T19:44:09.567 回答