如何给Python添加随机动作?
How to add random action to Python?
我一直在 Python 自己编写一个冒险游戏,通过 YouTube 和我在网上找到的东西,但我想添加一个部分,你必须上这艘船,但船票要 10 美元(你可以选择早点到。
但是如果你 没有 早点拿到 10,你还有另一种选择 运行 绕过要你付钱的人(我猜你甚至可以 if 你有 10 并且可以省钱)。但是如果你有 10 美元,你就通过,如果没有,你就重新开始,它 运行s sys.exit()
在撰写本文时,代码如下所示:
print("A man offers you a trip to the eastern side of the village via boat")
print(
"as a bridge has not been constructed yet, but it will cost , do you give him it ( Required) or try run past him(Free)")
check_inv = input()
if "" not in inv:
print("He caught you making a run for it! restart game")
sys.exit()
else:
print("Let's see if you have enough...")
print(inv)
print("You have enough and cross the river")
removeFrominventory("")
我知道如何编写随机数生成器,因为这是我被建议从事的另一个初学者项目,但我想成为如果你输入 'Run' 你将有 50/50 的机会能够打败运行他
假设您希望它像 pokemon 中的随机数生成器或想要创建抛硬币事件,您可以创建一个 list = [0,1]
列表并使用 random.choice(list)
或者您可以使用 randrange()
得到一个数字 b/w 0 和 100。假设超过的机会是 x%
。如果从 randrange 获得的值小于 x,则说明你跑得过,否则你没有。您可以创建如下函数:
def RNG(probability):
Generate Random num b/w 0 and 100
if num<probability:
return True
else:
return False
我更喜欢RNG功能。虽然它很耗时和内存,但它可以在代码中一次又一次地重复使用。
我一直在 Python 自己编写一个冒险游戏,通过 YouTube 和我在网上找到的东西,但我想添加一个部分,你必须上这艘船,但船票要 10 美元(你可以选择早点到。
但是如果你 没有 早点拿到 10,你还有另一种选择 运行 绕过要你付钱的人(我猜你甚至可以 if 你有 10 并且可以省钱)。但是如果你有 10 美元,你就通过,如果没有,你就重新开始,它 运行s sys.exit()
在撰写本文时,代码如下所示:
print("A man offers you a trip to the eastern side of the village via boat")
print(
"as a bridge has not been constructed yet, but it will cost , do you give him it ( Required) or try run past him(Free)")
check_inv = input()
if "" not in inv:
print("He caught you making a run for it! restart game")
sys.exit()
else:
print("Let's see if you have enough...")
print(inv)
print("You have enough and cross the river")
removeFrominventory("")
我知道如何编写随机数生成器,因为这是我被建议从事的另一个初学者项目,但我想成为如果你输入 'Run' 你将有 50/50 的机会能够打败运行他
假设您希望它像 pokemon 中的随机数生成器或想要创建抛硬币事件,您可以创建一个 list = [0,1]
列表并使用 random.choice(list)
或者您可以使用 randrange()
得到一个数字 b/w 0 和 100。假设超过的机会是 x%
。如果从 randrange 获得的值小于 x,则说明你跑得过,否则你没有。您可以创建如下函数:
def RNG(probability):
Generate Random num b/w 0 and 100
if num<probability:
return True
else:
return False
我更喜欢RNG功能。虽然它很耗时和内存,但它可以在代码中一次又一次地重复使用。