简体   繁体   English

为什么两个不同的数字在元组中返回相同的字符串?

[英]Why is it that two different numbers return the same string in a tuple?

I'm something of a newbie to Python coding and I've just been making short games to get into writing code more fluently. 我是Python编码的新手,我一直在做一些简短的游戏,以便更流畅地编写代码。 I have right now a "simulation" that is essentially a text-based fight between a hero and a goblin. 我现在有一个“模拟”,本质上是英雄与地精之间基于文本的战斗。 I am using a tuple to store the moves list and then calling on the elements in that tuple in a series of if statements. 我使用一个元组存储移动列表,然后在一系列if语句中调用该元组中的元素。 MY problem is that when the user enters the number 2, the "potion" move is used, but when the user enter 3, the "potion" move is also used. 我的问题是,当用户输入数字2时,将使用“药水”移动,但是当用户输入3时,还将使用“药水”移动。 The number 2 should trigger the "block" move, but does not. 数字2应该触发“阻止”移动,但不会触发。 I think this may have to do with my limited knowledge of tuples, but can anyone clarify this for me? 我认为这可能与我对元组的有限了解有关,但是有人可以为我澄清一下吗? Much appreciated. 非常感激。 The code is as follows... 代码如下...

#begins battle loop
while goblin > 0:

    hmoves = ('sword',
             'shield bash',
             'block',
             'potion')

    choice = int(input("\nEnter a number 0 - 3 to choose an attack: "))

    if hmoves[choice] is 'sword':
        print(name, "attacked with his sword!")
        goblin -= 3
        print("\ngoblin used bite!")
        hero -= 2
        print("Goblin HP:", goblin, "Hero HP:", hero)
    elif hmoves[choice] is 'shield bash':
        print(name, "used shield bash!")
        goblin -= 2
        print("\ngoblin used bite!")
        hero -= 2
        print("\nGoblin HP:", goblin, "Hero HP:", hero)
    elif hmoves[choice] is 'block':
        print(name, "used block!")
        print("\ngoblin used bite!")
        print("but it was blocked.")
        hero = hero
        goblin = goblin
        print("\nGoblin HP:", goblin, "Hero HP:", hero)
    elif hmoves[choice] is 'potion':
        print(name, "used a health potion.")
        hero += 4
        print("\ngoblin used bite!")
        hero -= 2
        print("\nGoblin HP:", goblin, "Hero HP:", hero)

    #print("Goblin HP:", goblin, "Hero HP:", hero)

if goblin <= 0:
    print("Congratulations you've completed the simulation.")
else:
    print("Sorry, you did not pass the simulation.")

You should change your stuff from is to == : 您应该将东西从is更改为==

goblin = 20
hero = 20
name = "lol"

#begins battle loop
while goblin > 0:

    hmoves = ('sword',
             'shield bash',
             'block',
             'potion')

    choice = int(input("\nEnter a number 0 - 3 to choose an attack: "))

    if hmoves[choice] == 'sword':
        print(name, "attacked with his sword!")
        goblin -= 3
        print("\ngoblin used bite!")
        hero -= 2
        print("Goblin HP:", goblin, "Hero HP:", hero)
    elif hmoves[choice] == 'shield bash':
        print(name, "used shield bash!")
        goblin -= 2
        print("\ngoblin used bite!")
        hero -= 2
        print("\nGoblin HP:", goblin, "Hero HP:", hero)
    elif hmoves[choice] == 'block':
        print(name, "used block!")
        print("\ngoblin used bite!")
        print("but it was blocked.")
        hero = hero
        goblin = goblin
        print("\nGoblin HP:", goblin, "Hero HP:", hero)
    elif hmoves[choice] == 'potion':
        print(name, "used a health potion.")
        hero += 4
        print("\ngoblin used bite!")
        hero -= 2
        print("\nGoblin HP:", goblin, "Hero HP:", hero)

Refer to the difference between is and ==. 请参阅is和==之间的区别。 The two strings are not necessarily the same object in memory, but they are same in terms of the characters. 这两个字符串在内存中不一定是相同的对象,但是就字符而言它们是相同的。 It will work sometimes though because of string interning , which is used for efficiency purposes. 尽管有时由于有效的字符串interning ,它仍然可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么相同的代码返回列表和元组 pandas? - Why same code return list and tuple pandas? 为什么两个不同对象上的哈希函数返回相同的值? - Why hash function on two different objects return same value? Python-为什么2个不同的函数将相同的元组标识为 <class 'tuple'> 而另一个函数将相同的元组标识为None - Python - Why 2 different functions identify the same tuple as <class 'tuple'> while another function identifies the same tuple as None 为什么我会为相同的算术运算得到两个不同的值? - Why am I getting two different values for the same arithmetic operation with the same numbers? 为什么numpy从同一个随机状态返回不同的随机数? - Why does numpy return different random numbers from the same random state? 为什么样本 function 在循环的第一次两次迭代期间返回相同的随机数集? - Why does sample function return same set of random numbers during 1st two iteration of loop? 用熊猫计数(同一行中有两个不同的数字) - Counting with pandas (two different numbers at the same row) 检查数字是否在两个不同数字的相同位置 - check if digit is at same position in two different numbers 两个不同的周数评估到相同的日期 - Two different week numbers evaluated to same date 为什么我的代码要比较两种不同排序算法的运行时间多次显示相同的数字? - Why is my code to compare the runtime of two different sorting algorithms displaying the same numbers multiple times?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM