0

NOTE: This is not a duplicate of

Python integer incrementing with ++

I triedw_sword += 1 and it still showed up as 0.

Okay so this is my code as of now.

#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()

Now as you can see, w_sword and w_shield are both = to 0 correct?

Right around where it says w_sword = +1 and w_shield = +1 is where I think the problem is.

When you run the program, enter c, and then craft a wooden sword/shield, it should automatically add 1 to w_sword/w_shield once you go to your (i)inventory. But for some reason when you go back to your (i)inventory, w_sword/w_shield (depending on which one you crafted) comes up as 0.

I thought this code would work mostly because in the items assignment, it contains str(w_sword) and str(w_shield) and the others. So when you craft and item like a wooden sword or wooden shield then it str(w_shield) or str(w_sword) should come up as 1.

Does anyone know what my problem is?? I use python 3.3 as said in the title.

Community
  • 1
  • 1

1 Answers1

2

The syntax is w_sword += 1 , not w_sword =+ 1.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • :( Sadly, I thought I finally saw something very helpful, so I tried it out, and the output when I went back to the inventory, was still 0 :c. I also tried out on shell animal = 0 print('craft animal') if ('craft animal'): animal = +1 then I entered in animal and the output was 1 I did it your way too, both worked, but for some reason it wasn't working on the other program. – Antonio Anselmo Jun 27 '15 at 17:53
  • Well, you initialize all of the variables in the `game()` method, so they reset to `0` everythime you're calling that method, You should initialize them outside of `game()`. – DeepSpace Jun 27 '15 at 17:58
  • I tried @DeepSpace approach to the problem but it would not work unless I put items with the variables (outside of game()) and it still didn't work, I suspect that items is supposed to stay inside of game() but it keeps saying local variable 'w_sword' referenced before assignment. – Antonio Anselmo Jun 27 '15 at 18:13
  • @AntonioAnselmo, `animal = 0` then `animal+=1` *increments* `animal`, but `animal =+1` *assigns* a positive 1 to animal. Try it starting with `animal = 1`. – Mark Tolonen Jun 27 '15 at 18:13
  • @MarkTolonen I know this is probably a stupid question but how do I inform that this post has been answered by your answer..? – Antonio Anselmo Jun 27 '15 at 18:19
  • @AntonioAnselmo, DeepSpace gave you the correct answer, so you can accept it. My comment is here and he or she is welcome to update the answer with my explanation. – Mark Tolonen Jun 27 '15 at 18:21