繁体   English   中英

python中的骰子滚动模拟

[英]dice roll simulation in python

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

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?!

这是一个完整的工作代码,它还处理输入的无效总和。 两个骰子的总和不能小于2或大于13。因此,对此条件进行检查会使您的代码更健壮。 在进入while循环之前,您需要初始化sumofRoll = 0 ,因为这首先需要进入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!!

我接过您的游戏,并添加了一些元素供您浏览。 一种是您可以使用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 

这是一个简单的while循环:

sumofRoll = -1; #a starting value!
while sumofRoll != input:
  #Put the code that updates the sumofRoll variable here
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))

使用do while语句

do:
dice1=randrange(1,7)
...
while(sumofroll != input)
print("all done") 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM