Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我如何编程这个骰子辊?_Python_Python 2.7 - Fatal编程技术网

Python 我如何编程这个骰子辊?

Python 我如何编程这个骰子辊?,python,python-2.7,Python,Python 2.7,所以我试着创建一个骰子,掷5次,取这5个值的和,从1到6。但是,如果其中任何一个值为2或5,则整个总和变为零。这就是我目前所拥有的 def score(hand): roll = random.randint(1,6) if roll == 2 or roll ==5: score = sum(roll * 6) print("("score") is 0") else: print("("sum") is your tot

所以我试着创建一个骰子,掷5次,取这5个值的和,从1到6。但是,如果其中任何一个值为2或5,则整个总和变为零。这就是我目前所拥有的

def score(hand):
    roll = random.randint(1,6)
    if roll == 2 or roll ==5:
        score = sum(roll * 6)
        print("("score") is 0")
    else:
        print("("sum") is your total")

不要妄下判断。我在尝试:(

你忘了问一个特定的问题,你的代码出了什么问题?你应该做什么?score=sum(roll*6)如果你正在努力格式化你的打印语句,有两种大致相同的方式。
打印(“分数是{score}”)。格式化(分数=分数))
打印(“分数是%s”%score)
print(“分数为{}”。格式(分数))
@khelwood,我实际上是想掷骰子*5,因为我试着掷骰子5次
def f():
    l = [ random.randint(1,6) for i in range(6)]
    print(l)
    if (2 in l or 5 in l):
        return 0
    else:
        return sum(l)


f()
[2, 1, 1, 4, 1, 1]
Out[120]: 
0



f()
[3, 3, 4, 4, 3, 3]
Out[121]: 
20