用Python制作RPG

时间:2014-11-14 22:46:01

标签: python

在用户输入后,代码卡在yes_or_no函数中。没有错误信息,请帮忙!正如你所看到的,我想做的就是完成一个简单的购买,我无法测试buy_something函数,而且我知道它可能有问题。

#!/usr/bin/env python

import time

# Intro
print "Input Name:"
time.sleep(1)
name = raw_input()
print "Welcome to Tittyland brave %s'" %(name)
time.sleep(2)
print "You are given nothing but 500 gold to start you journey..."
time.sleep(2)
print "Good luck..."
time.sleep(3)
print "Shopkeeper: 'Eh there stranger! Looks like you'll need some gear before going into the wild! Check out my store!'"
time.sleep(4)
print ""

#Inventory and first shop
inventory = {
    'pocket' : [],
    'backpack' : [],
    'gold' : 500,
}

shop = {
    'dagger' : 50,
    'leather armor' : 150,
    'broadsword' : 200,
    'health potion' : 75,   
}

#Buying items
for key in shop:
    print key
    print "price: %s" % shop[key]
    print ""
print "Shopkeeper: So, you interested in anything?"

answer1 = raw_input()
item = raw_input()

def buying_something(x):
    for i in shop:
        if shop[i] == x:
            inventory[gold] -= shop[i]
            inventory[backpack].append(shop[i])

def yes_or_no(x):   
    if x == 'yes':
        print "Shopkeeper: 'Great! So what is your desire stranger"
        buying_something(item)
    else:
        print "Shopkeeper: 'Another time then'"


yes_or_no(answer1)

2 个答案:

答案 0 :(得分:0)

在这一行之后会发生什么:

print "Shopkeeper: So, you interested in anything?"

你等待这个answer1 = raw_input()的原始输入 然后在您键入yes或no后立即再次等待输入item = raw_input()

Tt没有卡住或任何东西,它只是按照它所说的那样做。

print "Shopkeeper: So, you interested in anything?"

answer1 = raw_input()
item = raw_input() // <-- This is in the wrong place
yes_or_no(answer1)

您所写的内容要求用户在是或否答案后输入他们想要的项目,无论是或否。我建议你将item = raw_input()移到yes_or_no函数中。

def yes_or_no(x):   
    if x == 'yes':
        print "Shopkeeper: 'Great! So what is your desire stranger"
        item = raw_input()
        buying_something(item)
    else:
        print "Shopkeeper: 'Another time then'"

答案 1 :(得分:0)

我修复了你的两个功能。您的raw_inputs位于错误的位置:

def yes_or_no(purchase_q):
    if purchase_q == "yes":
        while True:
            things = raw_input("Great. What is your hearts desire(type no more to exit shop): ")
            if things != "no more":
                buying_something(things)
            else:
                print "Good luck on your journey then"
                break


def buying_something(item):
    if item in shop.keys():
        print "You have %s gold available" %(inventory.get('gold'))
        print "Item Added {0}: ".format(item)
        backpack_items = inventory.get('backpack')
        backpack_items.append(item)
        item_cost = shop.get(item)
        print "Cost of Item is %s gold coins " %(item_cost)
        inventory['gold'] = shop.get(item) - item_cost