迷你游戏帮助- 定义函数后无法打印

2024-04-23 20:43:14 发布

您现在位置:Python中文网/ 问答频道 /正文

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

所以我已经试过几百次了,我试过使用循环,还有很多其他的东西,但似乎没有什么能像我所希望的那样工作。大多数情况下,当我定义一个函数时,它在第一部分之后变为空白,它可能会更进一步,但我不知道。另外,从这个尴尬的问题可以看出,我是编程新手,请帮助我,如果有人能解释为什么它只打印文本到动作函数的定义。你知道吗


Tags: inyouforindexifhaverangefunction
1条回答
网友
1楼 · 发布于 2024-04-23 20:43:14

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

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开始。你知道吗

    如果对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.
    

相关问题 更多 >