0
    import random
#set counters
counter2=0
counter3=0
counter4=0
counter5=0
counter6=0
counter7=0
counter8=0
counter9=0
counter9=0
counter10=0
counter11=0
counter12=0
doubles=0

def main():
    #get input from user
    rolls = int(input("How many times would you like to roll the dice?")
    for count in range(rolls+1)
        #roll dice
        die1 = random.randint(1,6)
        die2 = random.randint(1,6)
        #add up dice totals to counters
        if die1 + die2 = 2:
            counter2 += 1
        if die1 + die2 == 3:
            counter3 += 1
        if die1 + die2 == 4:
            counter4 += 1
        if die1 + die2 == 5:
            counter5 += 1
        if die1 + die2 == 6:
            counter6 += 1
        if die1 + die2 == 7:
            counter7 += 1
        if die1 + die2 == 8:
            counter8 += 1
        if die1 + die2 == 9:
            counter9 += 1
        if die1 + die2 == 10:
            counter10 += 1
        if die1 + die2 == 11:
            counter11 += 1
        if die1 + die2 == 12:
            counter12 += 1
        if die1 == die2:
            doubles += 1
    #print data
    print("2 - ", counter2, \
          "3 - ", counter3, \
          "4 - ", counter4, \
          "5 - ", counter5, \
          "6 - ", counter6, \
          "7 - ", counter7, \
          "8 - ", counter8, \
          "9 - ", counter9, \
          "10 - ", counter10, \
          "11 - ", counter11, \
          "12 - ", counter12, \
          "Doubles - ", doubles)
main()

该程序的目标是掷 2 个骰子,无论用户想要多少次,然后列出骰子掷了多少次 1,2,3,4 等。行“die1 = random.randint(1,6)并且 die2 = random.randint(1,6) 给我一个语法错误并突出显示“die1”头脑告诉我我做错了什么?

4

4 回答 4

2

我稍微修正了你的程序。

请注意在嵌套连接调用中使用生成器表达式以提高效率。

print '\n'.join(['' if not globals().update({'rolls' : [(__import__("random").randint(1,6),__import__("random").randint(1,6)) for count in range(int(input("How many times would you like to roll the dice?")))]}) else '', '\n'.join("%d - %d" % (i, len(filter(lambda x: sum(x)==i, globals()["rolls"]))) for i in range(2,11)), "Doubles %d" % len(filter(lambda r:r[0]==r[1], globals()["rolls"]))]).lstrip()
于 2013-10-30T23:25:05.830 回答
1

几个错误:

  • if不带冒号的语句
  • 平等检验是==,不是=
  • 缺少右括号
  • 全局变量在函数中不可访问,将它们设为局部变量

所以这里是更正的版本:

import random

def main():
    #set counters
    counter2=0
    counter3=0
    counter4=0
    counter5=0
    counter6=0
    counter7=0
    counter8=0
    counter9=0
    counter10=0
    counter11=0
    counter12=0
    doubles=0

    #get input from user
    rolls = int(input("How many times would you like to roll the dice?"))
    for count in range(rolls+1):
        #roll dice
        die1 = random.randint(1,6)
        die2 = random.randint(1,6)
        #add up dice totals to counters
        if die1 + die2 == 2:
            counter2 += 1
        if die1 + die2 == 3:
            counter3 += 1
        if die1 + die2 == 4:
            counter4 += 1
        if die1 + die2 == 5:
            counter5 += 1
        if die1 + die2 == 6:
            counter6 += 1
        if die1 + die2 == 7:
            counter7 += 1
        if die1 + die2 == 8:
            counter8 += 1
        if die1 + die2 == 9:
            counter9 += 1
        if die1 + die2 == 10:
            counter10 += 1
        if die1 + die2 == 11:
            counter11 += 1
        if die1 + die2 == 12:
            counter12 += 1
        if die1 == die2:
            doubles += 1
    #print data
    print("2 - ", counter2, \
          "3 - ", counter3, \
          "4 - ", counter4, \
          "5 - ", counter5, \
          "6 - ", counter6, \
          "7 - ", counter7, \
          "8 - ", counter8, \
          "9 - ", counter9, \
          "10 - ", counter10, \
          "11 - ", counter11, \
          "12 - ", counter12, \
          "Doubles - ", doubles)
main()
于 2013-10-30T20:50:43.370 回答
0

在相等运算符==不是=

if die1 + die2 == 2:



rolls = int(input("How many times would you like to roll the dice?")) <- missing this parentheses
于 2013-10-30T20:45:48.843 回答
0

同意之前的答案,for循环和缺失存在问题:但是,如果您愿意接受defaultdict,您的代码可以大大简化。 这应该有助于防止进一步的语法错误,因为代码更加简洁且相对简单。主要区别在于defaultdict.

import random
from collections import defaultdict    


def main():
    counter = defaultdict(int)
    rolls = int(input("How many times would you like to roll the dice?"))
    for count in range(rolls):
        die1,die2 = random.randint(1,6),random.randint(1,6)
        counter[die1+die2]+=1
        if die1 == die2:
            counter['Doubles']+=1
    counter
    print('\n'.join('%s - %s '%(key,value) for key,value in counter.iteritems()))

当然,输出可以以任何您喜欢的方式格式化以满足您的需求,即排序、排序等

基本上在这种情况下,如果字典中不存在该键,则将创建一个键并将其分配为整数值 0。实际上,对代码的唯一更改是删除所有初始化并合并 if 检查。该数字只能是一个数值,并且可能是“双倍”。

于 2013-10-30T21:07:38.597 回答