变量在+1ed时不变(Python 3.3)

2024-06-09 12:49:17 发布

您现在位置:Python中文网/ 问答频道 /正文

在我之前的文章中,我说过每当我向变量中添加一些东西时,它不会改变。这是我当前的代码,我的问题就在下面

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

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

sticks = 2
stumps = 2
stone = 0
iron_in = 0
gold_in = 0
diamond = 0
platinum = 0
w_sword = 1
w_shield = 1
items = ('sticks:' + str(sticks) + ' stumps:' + str(stumps) + ' stone:' + str(stone) + ' iron ingot:' + str(iron_in) + ' gold ingot:' + str(gold_in) + ' diamond:' + str(diamond) + ' platinum:' + str(platinum) + ' Wooden sword(s):' + str(w_sword) +
' wooden shield(s):' + str(w_shield))  

def game():    

    #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 = 5

    monster_lvl = random.randint(1,10)

    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_swordw_shield应该以1开头,并且我的变量赋值应该在函数game()之外,所以我按照上面说的做了,每当我“制作”一把木剑或木盾时,它就会显示为1。但我没有意识到的是,无论我制作了什么(木剑或木盾),当我回到(I)库存时,w_swordw_shield都显示为1。例如 如果我在名为c_item的输入中键入wooden sword,然后返回到(I)inventory,w_swordw_shield都将显示为1,即使我只制作了一把木剑


Tags: togameifwsitemshieldprintcolored