2014-04-12 116 views
0

我想做一個模擬器小遊戲'因爲我很無聊,並且彈出一個問題,如果您嘗試購買catfood它將catfood添加到庫存,但不會將它添加到catfood計數,並且它doesn不要把錢帶走。我爲商店使用了一個單獨的模塊,而不是真正的遊戲文件,所以如果任何人都可以嘗試找到問題,我將不勝感激。這個python程序有什麼問題?

我叫商店功能是這樣的: shop(inv, balance, catfood, liqpota)

店鋪代碼:

from functions import * 
def shop(inv, balance, catfood, liqpota): 
while True: 
    print "Welcome to the shop." 
    print "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" 
    print "(To purchase an item; enter the letter of the item.)" 
    print "(If you want to exit; enter 'back'.)" 
    print "A] $5 Cat Food - 'Not Just For Cats'" 
    print "B] $7 Liquified Potatoes - 'Who Would Want These?'" 
    print 
    com = raw_input("Purchase: ") 
    divider() 
    if com == "back" or com == "Back": 
     break 
    elif com == "a" or com == "A": 
     if "Cat Food" in inv: 
      if balance < 7: 
       print "You have insufficient funds." 
      elif balance > 7 or balance == 7: 
       catfood = catfood + 1 
       balance = balance - 7 
       print "Purcahse succcessful." 
       return catfood 
       return liqpota 
     if not "Cat Food" in inv: 
      if balance < 7: 
       print "You have insufficient funds." 
      elif balance > 7 or balance == 7: 
       catfood = catfood + 1 
       balance = balance - 7 
       inv.append("Cat Food") 
       print "Purchase successful." 
       return catfood 
       return liqpota 
    elif com == "b" or com == "B": 
     print "WIP" 
     break 
    else: 
     print "Invalid Item/Command." 
    divider() 

主要代碼:(庫存件)

elif com == "inventory" or com == "Inventory": 
    tmp_invnum = 1 
    print "Cat Food = " + str(catfood) 
    print "Liquified Potatoes = " + str(liqpota) 
    print 
    for invf in inv: 
     print str(tmp_invnum) + "] " + invf 
     tmp_invnum += 1 
+0

所以'catfood = catfood + 1'和'balance = balance-7'這兩行不起作用?那個工作後的「打印」? – evamvid

回答

0

第二return語句(返程liqpota)從不執行,因爲你的函數在遇到的第一個返回時結束。您可以在單個語句中返回所有值,並在函數調用中解壓縮它們。我在下面包含了一個例子。

def divider(): 
    print '*' * 80 

def shop(inv, balance, catfood, liqpota): 
    while True: 
     print "Welcome to the shop." 
     print "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" 
     print "(To purchase an item; enter the letter of the item.)" 
     print "(If you want to exit; enter 'back'.)" 
     print "A] $5 Cat Food - 'Not Just For Cats'" 
     print "B] $7 Liquified Potatoes - 'Who Would Want These?'" 
     print 
     com = raw_input("Purchase: ") 
     divider() 
     if com == "back" or com == "Back": 
      break 
     elif com == "a" or com == "A": 
      if "Cat Food" in inv: 
       if balance < 7: 
        print "You have insufficient funds." 
       elif balance > 7 or balance == 7: 
        catfood = catfood + 1 
        balance = balance - 7 
        print "Purcahse succcessful." 
        # return all four variables at once 
        return (catfood,liqpota,inv,balance) 

      if not "Cat Food" in inv: 
       if balance < 7: 
        print "You have insufficient funds." 
       elif balance > 7 or balance == 7: 
        catfood = catfood + 1 
        balance = balance - 7 
        inv.append("Cat Food") 
        print "Purchase successful." 
        # return all four variables at once 
        return (catfood,liqpota,inv,balance) 

     elif com == "b" or com == "B": 
      print "WIP" 
      break 
     else: 
      print "Invalid Item/Command." 

     divider() 

    return (catfood,liqpota,inv,balance) 

catfood = 0 
liquified_potatoes = 0 
balance = 30 
inv = [] 

print 'catfood %s potatoes %s inventory %s balance %s' % (catfood,liquified_potatoes,inv,balance) 

catfood, liquified_potatoes, inv, balance = shop(inv, balance, catfood, liquified_potatoes)   

print 'catfood %s potatoes %s inventory %s balance %s' % (catfood,liquified_potatoes,inv,balance)