迷你游戏帮助2-在Raw_input之后执行相同的脚本部分

时间:2013-07-06 15:11:40

标签: python function

import time
import random
inventory = [""]
gold = 0
rawfish = ["Mackarel", "Cod", "Salmon", "Herring", "Tuna"]
trash = ["Old Shoe", "Plastic Bag", "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 "~~~~WELCOME TO FISHING~~~~"
time.sleep(2)
print "Loading Version 0.4 ..."
time.sleep(2)
print "In this current version the last item in your inventory is sold."

def sell_function():
    if inventory[0] in rawfish:
        sold = inventory.pop(0)
        gold += 5
        print "You have sold a", sold, "for 5 gold coins!"
    action_function()
    if inventory[0] in trash:
        sold = inventory.pop(0)
        gold += 1
        print "You have recycled a", sold, "for 1 gold coins!"
    action_function()
    if inventory[0] in special:
        sold = inventory.pop(0)
        gold += 10
        print "You have sold a", sold, "for 10 gold coins!"
    action_function()

def fish_function():
    if random.randrange == mackarel_range:
        inventory.append("Mackarel")
        print "You have reeled in a Mackarel!"
    action_function()
    if random.randrange == cod_range:
        inventory.append("Cod")
        print "You have reeled in a Cod!"
    action_function()
    if random.randrange == salmon_range:
        inventory.append("Salmon")
        print "You have reeled in a Salmon!"
    action_function()
    if random.randrange == herring_range:
        inventory.append("Herring")
        print "You have reeled in a Herring!"
    action_function()
    if random.randrange == tuna_range:
        inventory.append("Tuna")
        print "You have reeled in a Tuna!"
    action_function()
    if random.randrange == oldshoe_range:
        inventory.append("Old Shoe")
        print "You have reeled in an Old Shoe..."
    action_function()
    if random.randrange == plasticbag_range:
        inventory.append("Plastic Bag")
        print "You have reeled in a Plastic Bag..."
    action_function()
    if random.randrange == rustedemptybox_range:
        inventory.append("Rusted Empty Box")
        print "You have reeled in a Rusted Empty Box..."
    action_function()
    if random.randrange == plankfragment_range:
        inventory.append("Plank Fragment")
        print "You have reeled in a Plank Fragment..."
    action_function()
    if random.randrange == copperring_range:
        inventory.append("special1")
        print "You find a slightly dirty Copper Ring, after clearing the dirt it appears quite nice."
    action_function()

def action_function():
    chance = random.randrange(1,31)
    action = raw_input("Do you want to .sell or .fish?")
    if action == "sell":
        sell_function()
    if action == "fish":
        fish_function()
    if action == "inventory":
        print inventory
    action_function()

action_function()

我已经曾经向这个游戏寻求过帮助,我仍然想要一点一点地做出来。 因此,当我运行程序时,它似乎到达action = raw_input("Do you want to .sell or .fish?")部分它打印提示,但是当我键入fish或sell时,它再次打印提示。 :■ 在我看来,它只是回到函数的开头并重新启动它。 请帮忙。

1 个答案:

答案 0 :(得分:2)

您的问题出在if random.randrange == some_range检查中。实际上,您的代码有几个问题:

首先,您需要实际调用 randrange函数,并且它至少需要一个参数:范围的stop值(默认为从0开始) )。有关详细信息,请参阅the randrange documentation

其次,randrange()返回一个数字,但您将其结果与列表进行比较。这将始终False:在Python中,3[3] 不是相同的值。我怀疑你想要的是检查你的随机值是否包含在列表中。一种方法是if random_value in some_range,因为in运算符会搜索列表以查看它是否包含您传入的值。但这在很大范围内会很慢;更好的方法是停止使用范围并开始使用标记范围开始和结束的数字,然后执行if range_start <= random_value < range_end。请注意我在开始时使用<=和在结尾使用<的方式;这样你就可以把你的范围写成“从0到2,从2到5,从5到11”(或其他),每个可能的整数都属于一个且只有一个范围。

最后,每次拨打random.randrange()时,您都会收到不同的随机结果,因此您的代码完全有可能“捕获”多个项目,或者没有所有。相反,您可能想要这样做:

random_value = random.randrange()
if mackerel_start <= random_value < mackerel_end:
    print "Mackerel"
elif cod_start <= random_value < cod_end:
    print "Cod"
    # Etc.
else:
    print "Nothing"

这应该可以解决你钓鱼码所遇到的问题。至于你的销售代码,它看起来应该有效;之所以无所事事,是因为你的捕鱼代码从未在inventory列表中添加任何内容,因此无需出售。

编辑:还有一件事。不要再到处叫action_function()了;有一个更好的方法来做到这一点。使用if链来代替一长串if ... elif ... elif ... else语句,以确保其中一个(并且只有一个)可以正常工作。然后让函数返回结束。在action_function()内,使用while循环:

def action_function():
    while True:
        action = raw_input("Do you want to 'sell' or 'fish' or 'inventory' or 'quit'?")
        if action == "quit":
            break  # This ends the while loop
        if action == "sell":
            sell_function()
        if action == "fish":
            fish_function()
        if action == "inventory":
            print inventory

这将让你一直反复运行action_function()直到用户选择退出,然后它将结束程序。比点击Ctrl-C退出要优雅得多。

顺便说一句,我强烈建议您尽快阅读How to Think Like a Computer Scientist,并完成该书中的练习。完成该书后,您的编程技巧将会大大提高。最重要的是,你现在可以做的最好的事情。