Roll four six-sided dice
ignore the smallest
add up the remaining, and note the result
do over until you have got six values
[code=python]import random
for i in range(6):
dices = []
for i in range(4):
dices.append(ra ndom.randint(1, 6))
dices.sort()
temp = 0
for i in dices[1:]:
temp += i
print temp[/code]
for i in range(6):
dices = []
for i in range(4):
dices.append(ra ndom.randint(1, 6))
dices.sort()
temp = 0
for i in dices[1:]:
temp += i
print temp[/code]
Oh, I like the look of that. Now could you explain the temp part? And the dices.sort()...
While I like my class much better, this does the same thing without showing you too much about what's going on under the hood:[CODE=python]import random
def role(intList):
for i in range(len(intLi st)):
intList[i] = random.randint( 1,6)
While I like my class much better, this does the same thing without showing you too much about what's going on under the hood:[CODE=python]import random
def role(intList):
for i in range(len(intLi st)):
intList[i] = random.randint( 1,6)
dice = [i for i in range(4)]
role(dice)
print dice
print AddLowest(3, dice)
nTests = 6
for i in range(nTests):
role(dice)
print AddLowest(3, dice)
[/CODE]
I know I'm really really annoying now, but I still don't understand the AddLowest part. What is nItems supposed to be? And I haven't learned about "return" yet, but I suppose I can guess what it does.
While I like my class much better, this does the same thing without showing you too much about what's going on under the hood:[CODE=python]import random
def role(intList):
for i in range(len(intLi st)):
intList[i] = random.randint( 1,6)
for i in range(6):
dices = []
for i in range(4):
dices.append(ra ndom.randint(1, 6))
dices.sort()
temp = 0
for i in dices[1:]:
temp += i
print temp
[/code]
[code=python]
import random
for i in range(6):
dices = [random.randint( 1,6) for i in range(4)]
dices.sort()
print reduce(lambda x, y: x+y, dices[1:] )
[/code]
is a bit shorter tho
and:
[code=python]
for i in range(6): print reduce(lambda x, y: x+y, sorted([random.randint( 1,6) for i in range(4)])[1:] )
[/code]
is only one line. Totaly insane :D
for i in range(6):
dices = [random.randint( 1,6) for i in range(4)]
dices.sort()
print reduce(lambda x, y: x+y, dices[1:] )
[/code]
is a bit shorter tho
First, I don't know what reduce does, though it looks obvious, I can't define it in my head.
Second, it even says under my name at every post I make: Newbie. How am I supposed to know what lambda does? "Everybody shouts: GOOGLE IT!!!"
Well I did, and I did not understand it. Am I to stupid to make a script that rolls a couple of bloody dice and toys with them? Language minded...
Like some fellow said, the " # Comment/explanation for newbies here" is right handy!
for i in range(6):
dices = [random.randint( 1,6) for i in range(4)]
dices.sort()
print reduce(lambda x, y: x+y, dices[1:] )
[/code]
is a bit shorter tho
and:
[code=python]
for i in range(6): print reduce(lambda x, y: x+y, sorted([random.randint( 1,6) for i in range(4)])[1:] )
[/code]
is only one line. Totaly insane :D
First, I don't know what reduce does, though it looks obvious, I can't define it in my head.
Second, it even says under my name at every post I make: Newbie. How am I supposed to know what lambda does? "Everybody shouts: GOOGLE IT!!!"
Well I did, and I did not understand it. Am I to stupid to make a script that rolls a couple of bloody dice and toys with them? Language minded...
Nope, you're not too stupid! My new friend, Smygis, has thrown some pretty advanced stuff at you. Use one of the more spread out versions and take it apart (note that there are many way to achieve a task in programming)
Nope, you're not too stupid! My new friend, Smygis, has thrown some pretty advanced stuff at you. Use one of the more spread out versions and take it apart (note that there are many way to achieve a task in programming)
No offence, but according with my "level", your stuff was somewhat heavy too, even if I think I allmost got most of it now...
First, I don't know what reduce does, though it looks obvious, I can't define it in my head.
Second, it even says under my name at every post I make: Newbie. How am I supposed to know what lambda does? "Everybody shouts: GOOGLE IT!!!"
Well I did, and I did not understand it. Am I to stupid to make a script that rolls a couple of bloody dice and toys with them? Language minded...
Dont frinkin google it, look it up in the Python Reference Manual's.
You dont realy need lambda, reduce and the others (map, filter).
They are the evils of Python ;) dont go there. I however, like them.
Y'all can stop worrying now (yeah, as if) 'cause I solved it (Or rather, Smygis did)
I just stopped reading when he stopped making sense, and continued from there. In the end, it looked somewhat like this:
[code=python]
import random
for n in range(6)
dice=[random.randint( 1,6) for i in range(4)] # This was somewhat like when your maths-teacher does the worst exam-equation on the black-board, and you go, like. "Well of course, that was simple!" Though you know you'd have never figured it out by yourself.
dice.sort()
d2=dice[1:] # Probably a waste to not just go for editing dice rather than introducing d2, but I did not dare for fear of messing up...
print d2[0}+d2[1]+d2[2] # Could this be written: for n in d2: print n?
Y'all can stop worrying now (yeah, as if) 'cause I solved it (Or rather, Smygis did)
I just stopped reading when he stopped making sense, and continued from there. In the end, it looked somewhat like this:
[code=python]
import random
for n in range(6)
dice=[random.randint( 1,6) for i in range(4)] # This was somewhat like when your maths-teacher does the worst exam-equation on the black-board, and you go, like. "Well of course, that was simple!" Though you know you'd have never figured it out by yourself.
dice.sort()
d2=dice[1:] # Probably a waste to not just go for editing dice rather than introducing d2, but I did not dare for fear of messing up...
print d2[0}+d2[1]+d2[2] # Could this be written: for n in d2: print n?
[/code]
And that's the hard part all done!
Other then d2[0} shuld be d2[0]
it looks mutch better then the first one.
and if you wrote for n in d2: print n it wold simply print the numbers not add them together.
Comment