python骰子滚动重启脚本

时间:2016-10-17 19:59:44

标签: python

我想自动重启我的脚本我有一个骰子滚动脚本,我不想在每个滚动之间手动重置我的脚本这是我的脚本。

import random
from random import randrange
from random import randint
r = randint
min = 1
max = 6

rolls = int(float(input('how menny times do you want to roll:')))

for x in range(rolls):
    print ('Rolling the dices...')
    print ('The value is....')
    print (r(min, max))

我尝试了几种方法,但没有一种方法可以使用

1 个答案:

答案 0 :(得分:0)

我并不是100%确定你实际上要做什么,但这是我写的一个程序,它会一次滚动2个模具,并且会产生你想要滚动的许多卷。 - 不是使用递归的最佳实践(主要来自内部的主要调用),但它不是真正的问题,这是基本的。

*如果您使用的是Python 3+,请将raw_input替换为输入。我使用2.7所以我使用raw_input

import time
import random

def roll_multiple():
#honestly this is all you need to do what you want to do, the rest just turns it into a game and allows multiple runs
    roll_number = int(raw_input("How Many Times Would You Like to Roll?\n"))
    for i in range(roll_number):
        print"You Rolled A ", random.randrange(1,6,1), " and a ", random.randrange(1,6,1)
#that's it! if you do these 3 lines, it will ask for how many times, and for each item in range of roll_number (whatever you input) it will run the print below and generate new numbers for each roll.

def main():
    print("Welcome to Craps 2.0")
    playing = raw_input("Press Enter to Roll or Q to quit")

    if playing == 'q':
        print("Thanks for Playing")
        time.sleep(2)
        quit()
    elif playing != 'q':
        roll_multiple()
        main()
main()