用Python制作RPG
在用户输入之后,代码就卡在yes_或no函数中。没有错误消息,请帮助!正如你所看到的,我所要做的只是实现一个简单的购买,我还没有能够测试buy_某物功能,我知道它可能有问题用Python制作RPG,python,Python,在用户输入之后,代码就卡在yes_或no函数中。没有错误消息,请帮助!正如你所看到的,我所要做的只是实现一个简单的购买,我还没有能够测试buy_某物功能,我知道它可能有问题 #!/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
#!/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)
在这一行之后会发生什么:
print "Shopkeeper: So, you interested in anything?"
您使用此应答等待原始输入1=原始输入
然后,在键入“是”或“否”后,立即再次等待输入item=raw\u input
它没有被卡住或其他什么,它只是按照它说的去做
print "Shopkeeper: So, you interested in anything?"
answer1 = raw_input()
item = raw_input() // <-- This is in the wrong place
yes_or_no(answer1)
我修复了你的两个函数。您的原始输入位于错误的位置:
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
欢迎来到蒂蒂兰,嗯?不管怎样,你连续读两行输入。它不会被卡住,它只是等待第二行。作为规则,复制程序应该只包含演示问题所需的最小代码。在这种情况下,这就像是两行代码,而不是你的整个程序——也就是说,只有它卡住的地方。有关更多指南,请参阅。非常感谢,非常有帮助!
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