0

另一个与我的迷你游戏有关的问题。我在前面的问题上得到了很大的帮助,我希望我不会在这样一个很可能是愚蠢的问题上浪费你的时间。这是代码:

import time
import random
inventory = []
miningskill = 1
fishingskill = 1
gold = 0
rawfish = ["Mackarel", "Cod", "Salmon", "Herring", "Tuna"]
cookedfish = ["Cooked Mackarel", "Cooked Cod", "Cooked Salmon", "Cooked Herring", "Cooked Tuna"]
trash = ["Old Shoe", "Thin Bone", "Rusted Empty Box", "Plank Fragment"]
special = ["Copper Ring"]
mackarel_range = range(1,3)
cod_range = range(3,5)
salmon_range = range(5,7)
herring_range = range(7,9)
tuna_range = range(9,11)
oldshoe_range = range(11,16)
plasticbag_range = range(16,21)
rustedemptybox_range = range(21,26)
plankfragment_range = range(26,31)
copperring_range = range(31,32)

print"  _           _       _       _        ______ _     _     _ "            
print" | |         | |     (_)     ( )      |  ____(_)   | |   (_)"            
print" | |     ___ | |_ __  _ _ __ |/ ___   | |__   _ ___| |__  _ _ __   __ _ "
print" | |    / _ \| | '_ \| | '_ \  / __|  |  __| | / __| '_ \| | '_ \ / _` |"
print" | |___| (_) | | |_) | | | | | \__ \  | |    | \__ \ | | | | | | | (_| |"
print" |______\___/|_| .__/|_|_| |_| |___/  |_|    |_|___/_| |_|_|_| |_|\__, |"
print"               | |                                                 __/ |"
print"               |_|                                                |___/ "
time.sleep(2)
print".       .                                           .   .--."
print" \     /                o                         .'|       )"
print"  \   /   .-. .--..--.  .   .-. .--.       o        |    --:"
print"   \ /   (.-' |   `--.  |  (   )|  |                |       )"
print"    '     `--''   `--'-' `- `-' '  `-      o      '---'o`--'"
time.sleep(2)
print "In this current version the first item in your inventory is sold."

def sell_function():
    if inventory[0] in rawfish:
        sold = inventory.pop(0)
        global gold
        gold = gold+5
        print "You have sold a", sold, "for 5 gold coins!"
    elif inventory[0] in trash:
        sold = inventory.pop(0)
        global gold
        gold = gold+1
        print "You have recycled a", sold, "for 1 gold coins!"
    elif inventory[0] in special:
        sold = inventory.pop(0)
        global gold
        gold = gold+10
        print "You have sold a", sold, "for 10 gold coins!"
    elif inventory[0] in cookedfish:
        sold = inventory.pop(0)
        global gold
        gold = gold+8
        print "You have sold a", sold, "for 8 gold goins!"
    else:
        print "Shopkeeper:'You can't sell that.'" 

def fish_function_beginner():
    random_fishingchance = random.randrange(1,32)
    if 1 <= random_fishingchance < 3:
        inventory.append("Mackarel")
        print "You have reeled in a Mackarel!"
        time.sleep(0.5)
        print "You place it into your inventory"
        global fishingskill
        fishingskill = fishingskill + 1
        fishingskill_new = fishingskill
        print "Your fishing skill is now",fishingskill_new,"It has increased by 1"
    elif 3 <= random_fishingchance < 5:
        inventory.append("Cod")
        print "You have reeled in a Cod!"
        time.sleep(0.5)
        print "You place it into your inventory"
        global fishingskill
        fishingskill = fishingskill + 1
        fishingskill_new = fishingskill
        print "Your fishing skill is now",fishingskill_new,"It has increased by 1"
    elif 5 <= random_fishingchance < 7:
        inventory.append("Salmon")
        print "You have reeled in a Salmon!"
        time.sleep(0.5)
        print "You place it into your inventory"
        global fishingskill
        fishingskill = fishingskill + 1
        fishingskill_new = fishingskill
        print "Your fishing skill is now",fishingskill_new,"It has increased by 1"
    elif 7 <= random_fishingchance < 9:
        inventory.append("Herring")
        print "You have reeled in a Herring!"
        time.sleep(0.5)
        print "You place it into your inventory"
        global fishingskill
        fishingskill = fishingskill + 1
        fishingskill_new = fishingskill
        print "Your fishing skill is now",fishingskill_new,"It has increased by 1"
    elif 9 <= random_fishingchance < 11:
        inventory.append("Tuna")
        print "You have reeled in a Tuna!"
        time.sleep(0.5)
        print "You place it into your inventory"
        global fishingskill
        fishingskill = fishingskill + 1
        fishingskill_new = fishingskill
        print "Your fishing skill is now",fishingskill_new,"It has increased by 1"
    elif 11 <= random_fishingchance < 16:
        inventory.append("Old Shoe")
        print "You have reeled in an Old Shoe..."
        time.sleep(0.5)
        print "You place it into your inventory"
    elif 16 <= random_fishingchance < 21:
        inventory.append("Thin Bone")
        print "You have reeled in a Thin Bone..."
        time.sleep(0.5)
        print "You place it into your inventory"
    elif 21 <= random_fishingchance < 26:
        inventory.append("Rusted Empty Box")
        print "You have reeled in a Rusted Empty Box..."
        time.sleep(0.5)
        print "You place it into your inventory"
    elif 26 <= random_fishingchance < 31:
        inventory.append("Plank Fragment")
        print "You have reeled in a Plank Fragment..."
        time.sleep(0.5)
        print "You place it into your inventory"
    elif 31 <= random_fishingchance < 32:
        inventory.append("Copper Ring")
        print "You have reeled in a ring shaped object covered in mud."
        print "After cleaning it you notice it is a Copper Ring!"
        time.sleep(0.5)
        print "You place it into your inventory"
        global fishingskill
        fishingskill = fishingskill + 2
        fishingskill_new = fishingskill
        print "Your fishing skill is now",fishingskill_new,"It has increased by 2"
    else:
        print "It seems your fishing line has snapped!"

def fish_function_amateur():
    random_fishingchance = random.randrange(1,29)
    if 1 <= random_fishingchance < 3:
        inventory.append("Mackarel")
        print "You have reeled in a Mackarel!"
        time.sleep(0.5)
        print "You place it into your inventory"
        global fishingskill
        fishingskill = fishingskill + 1
        fishingskill_new = fishingskill
        print "Your fishing skill is now",fishingskill_new,"It has increased by 1"
    elif 3 <= random_fishingchance < 5:
        inventory.append("Cod")
        print "You have reeled in a Cod!"
        time.sleep(0.5)
        print "You place it into your inventory"
        global fishingskill
        fishingskill = fishingskill + 1
        fishingskill_new = fishingskill
        print "Your fishing skill is now",fishingskill_new,"It has increased by 1"
    elif 5 <= random_fishingchance < 7:
        inventory.append("Salmon")
        print "You have reeled in a Salmon!"
        time.sleep(0.5)
        print "You place it into your inventory"
        global fishingskill
        fishingskill = fishingskill + 1
        fishingskill_new = fishingskill
        print "Your fishing skill is now",fishingskill_new,"It has increased by 1"
    elif 7 <= random_fishingchance < 9:
        inventory.append("Herring")
        print "You have reeled in a Herring!"
        time.sleep(0.5)
        print "You place it into your inventory"
        global fishingskill
        fishingskill = fishingskill + 1
        fishingskill_new = fishingskill
        print "Your fishing skill is now",fishingskill_new,"It has increased by 1"
    elif 9 <= random_fishingchance < 11:
        inventory.append("Tuna")
        print "You have reeled in a Tuna!"
        time.sleep(0.5)
        print "You place it into your inventory"
        global fishingskill
        fishingskill = fishingskill + 1
        fishingskill_new = fishingskill
        print "Your fishing skill is now",fishingskill_new,"It has increased by 1"
    elif 11 <= random_fishingchance < 15:
        inventory.append("Old Shoe")
        print "You have reeled in an Old Shoe..."
        time.sleep(0.5)
        print "You place it into your inventory"
    elif 15 <= random_fishingchance < 19:
        inventory.append("Thin Bone")
        print "You have reeled in a Thin Bone..."
        time.sleep(0.5)
        print "You place it into your inventory"
    elif 19 <= random_fishingchance < 24:
        inventory.append("Rusted Empty Box")
        print "You have reeled in a Rusted Empty Box..."
        time.sleep(0.5)
        print "You place it into your inventory"
    elif 24 <= random_fishingchance < 29:
        inventory.append("Plank Fragment")
        print "You have reeled in a Plank Fragment..."
        time.sleep(0.5)
        print "You place it into your inventory"
    elif 29 <= random_fishingchance < 30:
        inventory.append("Copper Ring")
        print "You have reeled in a ring shaped object covered in mud."
        print "After cleaning it you notice it is a Copper Ring!"
        time.sleep(0.5)
        print "You place it into your inventory"
        global fishingskill
        fishingskill = fishingskill + 2
        fishingskill_new = fishingskill
        print "Your fishing skill is now",fishingskill_new,"It has increased by 2"
    else:
        print "It seems your fishing line has snapped!"

def action_function():
    while True:
        print "For a list of commands type 'help'"
        action = raw_input("What do you want to do? >")
        if action == "quit":
            break
            end
        if action == "sell":
            sell_function()
        if action == "fish":
            print "You throw your reel..."
            time.sleep(10)
            fish_skillcheck_function()
        if action == "inventory":
            print "You begin to open your inventory"
            time.sleep(0.5)
            print inventory
        if action == "money":
            print "You have",gold,"gold"
        if action == "gold":
            print "You have",gold,"gold"
        if action == "cook":
            fish_cookcheck_function()
        if action == "fishingskill":
            if 1 <= fishingskill < 75:
                print "Your fishing skill is",fishingskill
                print "Fishing Rank: Beginner"
            elif 75 <= fishingskill < 150:
                print "Your fishing skill is",fishingskill
                print "Fishing Rank: Amateur"
            elif 150 <= fishingskill < 300:
                print "Your fishing skill is",fishingskill
                print "Fishing Rank: Regular"
            elif 300 <= fishingskill < 500:
                print "Your fishing skill is",fishingskill
                print "Fishing Rank: Seasoned"
            elif 500 <= fishingskill < 750:
                print "Your fishing skill is",fishingskill
                print "Fishing Rank : Professional"
            elif 750 <= fishingskill < 1000:
                print "Your fishing skill is",fishingskill
                print "Fishing Rank : Expert"
            elif 1000 <= fishingskill < 9001:
                print "Your fishing skill is",fishingskill
                print "Fishing Rank : Fishing Grandmaster"    
            else:
                print "Your skill is not available."
        if action == "help":
            print "Commands are= 'help' 'quit' 'sell' 'fish' 'fishingskill' 'gold' 'money' 'cook' and 'inventory"
        if action == "cook":
            fish_cookcheck_function()

def fish_cookcheck_function():
    if inventory[0] == "Mackarel":
        cooked = inventory.pop(0)
        inventory.append("Cooked Mackarel") 
        print "You have cooked a", cooked
    elif inventory[0] == "Cod":
        cooked = inventory.pop(0)
        inventory.append("Cooked Cod") 
        print "You have cooked a", cooked
    elif inventory[0] == "Salmon":
        cooked = inventory.pop(0)
        inventory.append("Cooked Salmon") 
        print "You have cooked a", cooked
    elif inventory[0] == "Herring":
        cooked = inventory.pop(0)
        inventory.append("Cooked Herring") 
        print "You have cooked a", cooked
    elif inventory[0] == "Tuna":
        cooked = inventory.pop(0)
        inventory.append("Cooked Tuna") 
        print "You have cooked a", cooked
    else:
        "You can't cook this."
        action_function()

def fish_skillcheck_function():
    if 1 <= fishingskill < 75:
        fish_function_beginner()
    elif 75 <= fishingskill < 150:
        fish_function_amateur()
    else:
        print "My fishing level is too low"

action_function()

现在,根据我认为应该发生的情况,如果出现问题,例如库存中没有任何东西可以出售,那么它应该进入 else 部分

def sell_function():
        if inventory[0] in rawfish:
            sold = inventory.pop(0)
            global gold
            gold = gold+5
            print "You have sold a", sold, "for 5 gold coins!"
        elif inventory[0] in trash:
            sold = inventory.pop(0)
            global gold
            gold = gold+1
            print "You have recycled a", sold, "for 1 gold coins!"
        elif inventory[0] in special:
            sold = inventory.pop(0)
            global gold
            gold = gold+10
            print "You have sold a", sold, "for 10 gold coins!"
        elif inventory[0] in cookedfish:
            sold = inventory.pop(0)
            global gold
            gold = gold+8
            print "You have sold a", sold, "for 8 gold goins!"
        else:
            print "Shopkeeper:'You can't sell that.'"

然而,你们中的一些更高级的程序员可能已经注意到存在一些问题。这是我尝试使用空库存进行销售时收到的错误(类似于我尝试使用空库存进行烹饪时发生的情况):

Traceback (most recent call last):
  File "C:\Users\Lolpin\Desktop\fishinglooptest.py", line 308, in <module>
    action_function()
  File "C:\Users\Lolpin\Desktop\fishinglooptest.py", line 231, in action_function
    sell_function()
  File "C:\Users\Lolpin\Desktop\fishinglooptest.py", line 40, in sell_function
    if inventory[0] in rawfish:
IndexError: list index out of range

当我试图找到关于相同/相似错误的已回答问题时,我无法将它们放入我的代码的上下文中。._.

4

3 回答 3

3

Use a if inventory condition to check whether inventory is empty:

def sell_function():
     if inventory:
        if inventory[0] in rawfish:
        #other code 
     else:
        print "Shopkeeper:'You can't sell that.'"
于 2013-07-10T19:49:43.607 回答
1

The problem is that your inventory list is empty (so there is no 0th element). You can check for this by using if statement:

def sell_function():
    if not inventory:
        print "your inventory is empty! You cannot sell anything"
        return
    if inventory[0] in rawfish:
       ...
于 2013-07-10T19:47:37.910 回答
1

Also you can use a try/except to catch if your inventory has items

try:
    if inventory[0] in rawfish:
        #...all code
except IndexError:
    print "your inventory is empty"
于 2013-07-10T19:51:29.567 回答