【问题标题】:Making an RPG in Python用 Python 制作 RPG
【发布时间】:2015-01-12 10:37:10
【问题描述】:

代码在用户输入后立即卡在 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)

【问题讨论】:

  • “欢迎来到 Tittyland”,嗯?无论如何,您正在连续阅读两行输入。它不会卡住,它只是等待第二行。
  • 作为一项规则——复制器应该只包含演示问题所需的最少代码。在这种情况下,这将是两行,而不是整个程序——也就是说,只有卡住的地方。有关更多指南,请参阅 stackoverflow.com/help/mcve

标签: python


【解决方案1】:

我修复了你的两个函数。你的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

【讨论】:

  • 非常感谢,非常有帮助!
【解决方案2】:

在这行之后会发生什么:

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

你用这个answer1 = raw_input() 等待原始输入 然后在输入是或否后立即再次等待输入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'"

【讨论】:

    猜你喜欢
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多