python中的骰子滚动模拟

时间:2018-10-06 23:06:04

标签: python dice

我正在编写一个程序,该程序要反复掷两个骰子,直到达到用户输入的目标值。我不知道如何使掷骰子重复发生。请帮忙...

from random import randrange
print ("This program rolls two 6-sided dice until their sum is a given target value.")
input = int (input("Enter the target sum to roll for:"))

#def main():

dice1 = randrange (1,7)

dice2 = randrange (1,7)

sumofRoll = dice1 + dice2

output = "Roll: {} and {}, sum is {}".format (dice1,dice2,sumofRoll)
print (output)

if sumofRoll == input:
    print ("All done!!")
if sumofRoll != input:


#how do I make this repeatedly run until input number is reached?!

5 个答案:

答案 0 :(得分:1)

这是一个完整的工作代码,它还处理输入的无效金额。两个骰子的总和不能小于2或大于13。因此,对此条件进行检查会使您的代码更健壮。您需要先初始化sumofRoll = 0,然后再进入while循环,因为这首先需要进入while循环。值为0是安全值,因为我们排除了用户输入的0的值作为有效和。

from random import randrange 
print ("This program rolls two 6-sided dice until their sum is a given target value.") 
input_sum = int(input("Enter the target sum to roll for:"))

def main():
    sumofRoll = 0
    if input_sum < 2 or input_sum > 13:
        print ("Enter a valid sum of dices")
        return
    while sumofRoll != input_sum:
        dice1 = randrange (1,7)
        dice2 = randrange (1,7)
        sumofRoll = dice1 + dice2
        output = "Roll: {} and {}, sum is {}".format (dice1,dice2,sumofRoll) 
        print (output)
        if sumofRoll == input_sum: 
            print ("All done!!") 

main()  

This program rolls two 6-sided dice until their sum is a given target value.
Enter the target sum to roll for:10
Roll: 3 and 5, sum is 8
Roll: 6 and 6, sum is 12
Roll: 5 and 1, sum is 6
Roll: 2 and 5, sum is 7
Roll: 6 and 6, sum is 12
Roll: 3 and 5, sum is 8
Roll: 1 and 2, sum is 3
Roll: 6 and 4, sum is 10
All done!!

答案 1 :(得分:1)

我接过您的游戏,并添加了一些元素供您浏览。一种是您可以使用random.choice并从die列表中进行选择,而不必重复生成新的随机整数。其次,您可以使用try/except块仅接受int(2, 13)范围内的一个。接下来,我们可以添加roll_count来跟踪rolls达到目标的数量。我们的while循环将持续到target == roll_sum,然后我们才能print得出结果。

from random import choice

die = [1, 2, 3, 4, 5, 6]

print('This program rolls two 6-sided dice until their sum is a given target.')
target = 0
while target not in range(2, 13):
    try:
        target = int(input('Enter the target sum to roll (2- 12): '))
    except ValueError:
        print('Please enter a target between 2 and 12')

roll_sum = 0
roll_count = 0

while target != roll_sum:

    die_1 = choice(die)
    die_2 = choice(die)
    roll_sum = die_1 + die_2
    roll_count += 1
    print('You rolled a total of {}'.format(roll_sum))

print('You hit your target {} in {} rolls'.format(target, roll_count))
...
You rolled a total of 4
You rolled a total of 12
You hit your target 12 in 29 rolls

答案 2 :(得分:0)

这是一个简单的 while 循环:

sumofRoll = -1; #a starting value!
while sumofRoll != input:
  #Put the code that updates the sumofRoll variable here

答案 3 :(得分:0)

state

答案 4 :(得分:0)

使用do while语句

count = 0
while(sumOfRoll != input):
    dice1 = randrange(1,7)
    dice2 = rangrange(1,7)
    count = count + 1
    sumOfRoll = dice1 + dice2
print("sum = %s, took %s tries" % (sumOfRoll, count))