首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在循环中重置变量的值?(Python 3.1.1)

如何在循环中重置变量的值?(Python 3.1.1)
EN

Stack Overflow用户
提问于 2013-08-19 23:40:36
回答 3查看 8.3K关注 0票数 0

当我运行我的代码并进入我游戏的战斗部分时,我会得到分配给我的角色攻击、防御、伤害和健康的随机值。然而,在他们的第一轮,他们得到相同的值,他们不能重置。

例如,用户的攻击是一个从4到11的随机数。

代码语言:javascript
复制
userAtk = random.randint(4,11)

我认为,每次循环运行时,它都会生成一个新值。但情况并非如此,每次我打印变量时,它的值都与第一次分配的值相同。我是不是遗漏了什么?

下面是我的代码

代码语言:javascript
复制
import random

# VARIABLES
#
#
# This variable is the user's character name
userName = input("Brave warrior, what is your name? ")

# This variable is used in the input function to pause the game
enterNext = ("Press enter to continue...")

# This variable is used to format the user input prompt
# as well as the battle text
prompt = ">>>>>  "

# This variable is used to add a new line to a string
newLine = "\n"

# This variable is used to display a message when the hero dies
heroDeadmsg = userName + " has fallen!"

# These variables represent the dragon's stats (HP, ATK & DEF)
dragonHp = 100
dragonAtk = random.randint(5,10)
dragonDef = random.randrange(8)

# These variables represent the user's stats (HP, ATK & DEF)
userHp = 90
userAtk = random.randint(4,11)
userDef = random.randrange(8)

# These variables calculate battle damage and HP
dragonDmg = (userAtk - dragonDef)
dragonHp -= dragonDmg
userDmg = (dragonAtk - userDef)
userHp -= userDmg

# This variable prints the options in the battle menu
battleMenu = """Attack (a) - Magic (m) - Item (i) - Run (r)""" 

# This variable determines who goes first
cointoss = random.randint(0, 1)

# These variables print the actions in each turn
dragonAttack = \
    prompt + "Crimson Dragon attacks you with " + str(dragonAtk) + " ATK!"\
    + newLine + prompt + "You defend with " + str(userDef) + " DEF!"\
    + newLine + prompt

userAttack = \
    prompt + "You attacked with " + str(userAtk) + " ATK!"\
    + newLine + prompt + "Crimson Dragon defends with " + str(dragonDef) + " DEF!"\
    + newLine + prompt

userMagic = \
    prompt + userName + " tried to use Magic!"\
    + newLine + prompt + userName + " has no magic!"\
    + newLine + prompt

userItem = \
    prompt + userName + " tried use an Item!"\
    + newLine + prompt + userName + " has no Items!"\
    + newLine + prompt

userRetreat = \
    prompt + userName + " tries to retreat!"\
    + newLine + prompt + "The enemy won't let you escape!"\
    + newLine + prompt


# These variables show health during battle 
printDragonhp = "Crismon Dragon has " + str(dragonHp) + " HP remaining!"
printUserhp = userName + " has " + str(userHp) + " HP remaining!"

# This variable simulates the results of a coin toss
coinToss = random.randint(0, 1)


#
#
# CONDTITIONS
#
#
# These conditions determines who attacks first
if coinToss == 0:
    currentTurn = "dragon"
elif coinToss == 1:
    currentTurn = "user"
else:
    print("The Coin Toss Failed!")    

#
#
# BATTLE MECHANICS
#
#

while currentTurn:
# Mechanics for the Crimson Dragon's Turn
if currentTurn == "dragon":

    # Prints the Crimson Dragon's Attack and ends the turn
    print(newLine + prompt + "Crimson Dragon moves!"\
          + newLine + prompt + newLine + dragonAttack\
          + newLine + prompt + userName + " takes " + str(userDmg) + " DMG!"\
          + newLine + prompt + printUserhp)
    currentTurn = "user"
    input(prompt)

    # Need to implent a way to reset ATK and DEF

# Mechanics for the User's Turn    
if currentTurn == "user":

    # Prints the Battle Menu and asks for the User's choice
    print(newLine + prompt + battleMenu\
          + newLine + prompt)
    userChoice = input(prompt)

    # Prints the User's Attack and ends the turn
    if userChoice == "a":
        print(userAttack)
        if userHp < 1:
            print(heroDeadmsg)
            break

        input (prompt)
        currentTurn = "dragon"
    # Prints the User's Magic and ends the turn
    elif userChoice == "m":
        print(userMagic)
        input (prompt)
        currentTurn = "dragon"
    # Prints the User's Item and ends the turn   
    elif userChoice == "i":
        print(userItem)
        input (prompt)
        currentTurn = "dragon"
    # Prints the User's Retreat and ends the turn    
    elif userChoice == "r":
        print(userRetreat)
        input (prompt)
        currentTurn = "dragon"
    # Prints an error message for invalid entries
    else:
        print(newLine + prompt + "That is not a valid menu item."\
              + newLine + prompt + "Please try again.")
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-08-20 01:59:16

random.randint(4,11)只是在范围[4, 11]中选择一个整数并返回这个数字。因此,当您执行userAtk = random.randint(4,11)时,您只需获取一个数字并将其存储为userAtk,每次访问userAtk时,您都会得到相同的号码。

如果您希望userAtk是一种神奇的东西,每次您访问它时,在范围[4, 11]中都会表现出一个不同的数字。嗯,这并不是不可能的(见这里,快速而肮脏地尝试一下)…但这几乎肯定会导致更多的混乱,而不是利益。

例如,您有试图打印出str(userAtk)…的代码。但是,如果每次访问它时,值是不同的,则打印出来的内容将与用于计算损失的内容不同!想象一下,如果你正在玩桌面D&D,而地牢的主人滚动一个模具告诉你你的滚动,然后立即忘记了结果,并再次滚动,以确定你是否击中。所以他可能会说,“你滚了一个20,你错过了。”这不太好。

可能有用的是使userAtk实际上是一个函数:

代码语言:javascript
复制
def userAtk():
    return random.randint(4, 11)

对于所有相似的变量也是如此。然后,只要访问一个数字,就可以调用该函数:

代码语言:javascript
复制
def dragonDmg():
    return userAtk() - dragonDef()

然后,在某个地方,您需要将调用这些函数的结果存储在每个循环中的一些局部变量中。

但关键是,不管你怎么做,你必须有变量,你每次都要通过循环重新计算。

票数 3
EN

Stack Overflow用户

发布于 2013-08-19 23:47:30

因为据我所见,userAtk不在循环中。如果希望在循环中重置它,请在循环中调用random.randint(4,11)

票数 2
EN

Stack Overflow用户

发布于 2013-08-20 00:31:52

我相信,即使你给布兰特打了一百万次电话,你还是会得到重复的(我相信你知道这一点)。我过去经常使用字典来跟踪使用过的/未使用的随机数,并检查字典是否已经使用过。可能误解了这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18324771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档