【发布时间】:2017-08-29 12:40:21
【问题描述】:
我正在使用 Python 编写一个非常基本的掷骰子程序,目前正在添加一个 ETA 系统(要求程序掷骰子 1000000 次以上需要一段时间,有些人可能会认为它崩溃了)和我的系统'已经想到了以下内容:
由于“骰子”是通过生成一个随机数并在 for 循环中重复来“滚动”的,如果我将变量取出并在一秒钟后将其与变量进行比较,我可以做一些基本的数学运算来猜测剩余时间.
我的问题是如何在不完全冻结程序的情况下在计数之间等待一秒钟(time.sleep)。
任何帮助将不胜感激,谢谢!
代码:
import random
finished = 0
printresults = 0
dicesides = 0
rolls = 0
amountcompleted = 0
while finished !="n":
finished = 0
printresults = 0
dicesides = 0
rolls = 0
amountcompleted = 0
rollsdone = 0
countlist = []
rolls = int(input("How many times should the dice be rolled? ")) #Variable that counts how many times the dice should be rolled
dicesides = int(input("How many sides does the dice have? ")) #Variable that contains how many sides the dice has
while printresults !="y" and printresults !="n":
printresults = input("Print dice roll results as they're created? say y/n ") #If user says y, result will be printed as results are made
if printresults !="y" and printresults !="n":
print("Answer invalid")
while amountcompleted !="y" and amountcompleted !="n":
amountcompleted = input("Print the amount of rolls completed as the dice is rolled? (Reccomended when rolling over 1M times) answer y/n ")
if amountcompleted !="y" and amountcompleted !="n":
print("Answer invalid")
for counter in range(0,dicesides): #Creates list of the right length to hold results
countlist.append(0)
for counter in range (0,rolls): #Main bit of the script that actually calculates and stores amount of dice rolls
number = random.randint(1,dicesides) #"Rolls" the dice, using the dicesides variable.
if printresults == "y":
print(number) #Prints the results as they are made if enabled
if amountcompleted == "y":
(rollsdone) = int(rollsdone + 1)
print("Completed {0} rolls".format((rollsdone)))
for counter in range (0,dicesides + 1): #For variable to store the results in a list
if number == counter:
countlist[counter-1] = countlist[counter-1] + 1 #Adds 1 to the right bit of the list
for counter in range(0,dicesides):
print("Number of {0}s: {1}".format(counter + 1,countlist[counter])) #Prints results
while finished != "y" and finished != "n":
finished = input("Reroll? Answer y/n ") #Asks the user if they want to reroll with different settings
if finished != "y" and finished != "n":
print("Input invalid")
【问题讨论】:
-
你到底想要什么?你想要时间过去吗?
-
显示代码。如果您想在计数之间等待一秒钟并掷出 1000000+ 次骰子,那么这就是 1000000+ 秒,对吧?