[英]storing a randomly generated number in python which can be called at any time
我想要做的是讓 Python 生成一個介於 1 和 6 之間的數字,然后將 6 添加到我要做的那個數字並為我提供結果,但是我似乎無法弄清楚如何制作可以調用該值,因此它可以在游戲過程中上下波動,這是我目前所擁有的:
import random
import time
Name = input("Before we get started lets get your character created, tell me what is your name?")
print()
print ("Welcome ",Name," to the adventure of a lifetime. The next thing you will need to do is sort out your character stats. This will require dice rolls which we will do within the game.")
print()
def skillroll():
skillroll=""
while skillroll !="Y" and skillroll != "N":
skillroll = input("First we need to roll for your skill, would you like me to roll? Y/N")
if skillroll=="Y":
print("Rolling the dice..")
skill=random.randint(1,6)
time.sleep(2)
print("I rolled a", skill, " I will now add 6 to this so your skill is", skill+6)
skill=skill+6
print()
return skillroll
skillroll()
我只是看不出如何得出最終答案,以便我可以在玩游戲時使用它。
我的一個朋友發給我這個幫助https://github.com/protocol7/python-koans/blob/master/python%202/koans/about_classes.py
但我只是看不出這有什么關系,我在 Stackoverflow 上找到的每個答案都是針對不同語言的。
只需使用:
import random
random.randrange(1, 7)
得到 1 到 6 之間的任何數字。
您的代碼變為:
import random
import time
def skillroll():
skillroll=""
while skillroll !="Y" and skillroll != "N":
skillroll = input("First we need to roll for your skill, would you like me to roll? Y/N")
if skillroll=="Y":
print("Rolling the dice..")
skill = random.randrange(1, 7)
time.sleep(2)
print("I rolled a ", skill, ". I will now add 6 to this so your skill is", skill+6, "\n")
skill=skill+6
return skillroll # What if N??
Name = input("Before we get started lets get your character created, tell me
what is your name?")
print ("\nWelcome ",Name," to the adventure of a lifetime. The next thing you
will need to do is sort out your character stats. This will require dice
rolls which we will do within the game.\n")
skillroll()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.