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”头脑告诉我我做错了什么?
答案 0 :(得分: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()
答案 1 :(得分: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()
答案 2 :(得分:0)
是==
而不是=
if die1 + die2 == 2:
rolls = int(input("How many times would you like to roll the dice?")) <- missing this parentheses
答案 3 :(得分: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检查。该数字只能是一个数字值,可能是“双打”。