Reputation: 1
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()
the object of the program is to roll the 2 dice however many times the user wants, then list how many times the dice rolled 1,2, 3, 4, etc etc. the lines "die1 = random.randint(1,6) and die2 = random.randint(1,6) give me a syntax error and highlight the "die1" mind telling me what i'm doing wrong?
Upvotes: 0
Views: 552
Reputation: 268
I fixed up your program a bit.
Notice the use of a generator expression in the nested join call for extra efficiency.
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()
Upvotes: 2
Reputation: 1095
Agreeing with the previous answers there is an issue with the for
loop and missing :
.
However your code can be greatly simplified if you are willing to embrace defaultdict
This should help prevent further syntax errors as the code is much more condensed and relatively simple. The main difference is utilisation of the 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()))
Of course the output can be formatted any way you like to to suite your needs, ie ordered, sorted, etc
Basically in this case if the key does not exist in the dictionary one will be created and assigned the integer value of 0. Really the only change to your code was removing all the initialization and consolidating the if checks. The number can only ever be one numeric value and potentially 'Doubles'.
Upvotes: 0
Reputation: 18927
A few errors:
if
statement without colon==
, not =
So here's the corrected version:
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()
Upvotes: 1
Reputation: 8685
in equality operator is ==
not =
if die1 + die2 == 2:
rolls = int(input("How many times would you like to roll the dice?")) <- missing this parentheses
Upvotes: 0