[英]How to make to variables to subtracted each other in python
I was trying to make a basic RNG combat, so people could just copy and paste it and use for their games at our school but I need help on one part. 我试图进行基本的RNG战斗,所以人们可以将其复制并粘贴并用于我们学校的游戏中,但我需要一方面的帮助。
import random
print("Your Weapon's Stats")
print(" /^\\ ")
print(" | | ")
print(" | | ")
print(" | | ")
print(" | | ")
print(" | | ")
print(" \\\=*=// ")
print(" | ")
print(" (+) ")
print("(+)~~~~~~~~~~~~~~~(+)")
print(" | Damage: | ")
print(" | 1-9 | ")
print(" | Attack Speed: | ")
print(" | 6/10 | ")
print(" | Critical Chance:| ")
print(" | 64% | ")
print("(+)~~~~~~~~~~~~~~~(+)")
your_damage = random.choice("12345789")
enemy_health = 20
enemy_health - your_damage <---it says that not right so what do I do?
print(enemy_health)
You need to use the -=
operator to subtract your_damage
from enemy_health
: 你需要使用-=
运算符减去your_damage
从enemy_health
:
enemy_health -= your_damage
This is equivalent to writing enemy_health = enemy_health - your_damage
. 这等效于写出enemy_health = enemy_health - your_damage
。
You also want to change random.choice("12345789")
to random.randint(1, 9)
. 您还想将random.choice("12345789")
更改为random.randint(1, 9)
。 This selects a random integer between 1
and 9
inclusive, thus ensuring that your_damage
is a number instead of a string. 这将选择一个介于1
到9
之间(含1
和9
的随机整数,从而确保your_damage
是数字而不是字符串。
Your code should be: 您的代码应为:
import random
print("Your Weapon's Stats")
print(" /^\\ ")
print(" | | ")
print(" | | ")
print(" | | ")
print(" | | ")
print(" | | ")
print(" \\\=*=// ")
print(" | ")
print(" (+) ")
print("(+)~~~~~~~~~~~~~~~(+)")
print(" | Damage: | ")
print(" | 1-9 | ")
print(" | Attack Speed: | ")
print(" | 6/10 | ")
print(" | Critical Chance:| ")
print(" | 64% | ")
print("(+)~~~~~~~~~~~~~~~(+)")
your_damage = random.randint(1, 9)
enemy_health = 20
enemy_health -= your_damage
print(enemy_health)
You aren't actually assigning your new value to enemy_health
, so you want to do this: 您实际上并未将新值分配给enemy_health
,所以您想这样做:
enemy_health = enemy_health - your_damage
Which can be simplified using the -=
operator. 可以使用-=
运算符简化。
So: 所以:
enemy_health -= your_damage
To comment about your usage of random.choice
. 评论您对random.choice
的使用。 You are almost correct in what you are trying to do, however, you want to use a list of integers instead of a string of numbers. 您尝试做的事几乎是正确的,但是,您要使用整数列表而不是数字字符串。 So you can do this instead: 因此,您可以这样做:
random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Even nicer: 更好:
random.choice(range(1, 10))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.