2015-06-27 38 views
0

注: 這不是如何+1以前的分配(Python的3.3)

Python integer incrementing with ++

重複我試圖w_sword += 1,它仍然出現了爲0

好了,所以這是我現在的代碼。

#creates random monsters with xp points, leveling up player, and adding upgrades 

#imports 
from clint.textui import colored, puts 
import random 

def game(): 
    sticks = 2 
    stumps = 2 
    stone = 0 
    iron = 0 
    gold = 0 
    diamond = 0 
    platinum = 0 
    w_sword = 0 
    w_shield = 0 

    items = ('sticks:' + str(sticks) + ' stumps:' + str(stumps) + ' stone:' + str(stone) + ' iron:' + str(iron) + ' gold:' + str(gold) + ' diamond:' + str(diamond) + ' platinum:' + str(platinum) + ' Wooden sword(s):' + str(w_sword) + 
    ' wooden shield(s):' + str(w_shield))  

    #start of the game  
    def start_game(): 
     print('   Hello player! Welome to a text based game involving killing monsters, leveling up, crafting weapons, and upgrading weapons!!!') 
     print(' To get started enter in (i)inventory (c)craft items (d)description of items (m)types of monsters or (f)fight monsters.') 
     print('     ENTER (help) FOR HOW THE GAME WORKS') 
     print('        ENTER(?) FOR TO PRINT THIS AGAIN') 
     print(' WHILE YOU ARE IN A CATEGORY SUCH AS (i)inventory PRESS (?) CAN BE USED TO GO BACK AND GO TO ANOTHER CATEGORY') 
    start_game() 

    main_In = input() 

    level = 0 
    Pxp = 0 
    gold = 0 

    if main_In == ('c'): 
     ws_sticks = 2 
     ws_stumps = 1 

     def craft(): 
      print('Would you like to craft an item??') 
      print('(Red = uncraftable)') 
      print('(Green = craftable)') 
      print('(Type items in lowercase)') 

      if sticks < ws_sticks: 
       puts(colored.red('Wooden sword')) 
      else: 
       puts(colored.green('Wooden sword')) 
      if stumps < ws_stumps: 
       puts(colored.red('Wooden shield')) 
      else: 
       puts(colored.green('Wooden shield')) 

     craft() 
     C_item = input() 

     def re_craft(): 
      print('press ENTER to go back to the start screen, or enter c to craft an(other) item.') 
      cor_go = input() 
      if cor_go == ('c'): 
       craft() 
       C_item = input() 
      else: 
       game() 

     if C_item == ('no'): 
      print('press ENTER to go back to the start screen') 
      input() 
      game() 

     if C_item == ('wooden sword') and sticks >= ws_sticks: 
      print('Wooden sword added to your inventory') 
      re_craft() 

     if C_item == ('wooden sword') and sticks < ws_sticks: 
      print('You need ' + str(ws_sticks - sticks) + ' stick(s) to craft a wooden sword') 
      re_craft() 

     if C_item == ('wooden shield') and stumps >= ws_stumps: 
      print('Wooden shield added to your inventory') 
      re_craft() 

     if C_item == ('wooden shield') and stumps < ws_stumps: 
      print('You need' + str(ws_stump - stumps) + ' stumps to craft a wooden shield.') 
      re_craft() 
     while ('Wooden shield added to your inventory'): 
      w_shield = +1 
     while ('Wooden sword added to your inventory'): 
      w_sword = +1 

    if main_In == ('i'): 
     puts(colored.yellow('Level: ' + str(level))) 
     puts(colored.yellow('xp: ' + str(Pxp))) 
     puts(colored.yellow('gold:' + str(gold))) 
     puts(colored.yellow('Items: ' + str(items))) 
     puts(colored.yellow('press ENTER to return to the start screen')) 
     input() 
     game() 

    if main_In == ('?'): 
     game() 

game() 

現在,您可以看到,w_sword和w_shield都= 0到0正確嗎?

對周圍的地方說w_sword = +1和w_shield = +1是我認爲這個問題是。

當你運行該程序,輸入c,然後制定一個木劍/盾,它應該當你去你的(我)庫存自動加1到w_sword/w_shield。但由於某種原因,當您返回到您的(i)庫存時,w_sword/w_shield(取決於您製作的是哪一個)出現爲0.

我認爲此代碼的工作原理主要是因爲在項目分配中, str(w_sword)和str(w_shield)等。所以,當你的工藝和項目如木劍或木製盾牌那麼STR(w_shield)或STR(w_sword)也將作爲1

有誰知道我的問題是什麼? 我在標題中使用了python 3.3。

+2

你的意思是做'w_sword + = 1'嗎?的 – Pynchia

+0

可能重複[Python的整數,++遞增(http://stackoverflow.com/questions/2632677/python-integer-incrementing-with) – IanAuld

回答

2

的語法w_sword += 1,不w_sword =+ 1

+0

:(可悲的是,我想我終於看到了一些非常有幫助的,所以我嘗試出來,輸出時,我又回到了庫存,仍然是0:C我也嘗試了上殼 動物= 0 打印(「手藝動物」) 如果(「手藝動物」): 動物= +1 後來我在 動物 進入輸出功率爲我做了太多自己的方式,無論是工作,但由於某種原因,它不工作的其他程序。 –

+0

好吧,你的ini對'game()'方法中的所有變量進行tialize,所以它們在你調用該方法的每一天都重置爲'0',你應該在'game()'之外初始化它們。 – DeepSpace

+0

我試圖@DeepSpace解決問題的方法,但它不會工作,除非我把物品與變量(外遊戲()),它仍然沒有工作,我懷疑是項目應該呆在遊戲裏(),但它在分配之前一直說本地變量'w_sword'被引用。 –