Hello, I have a problem that's hugely annoying. The other day, me and a friend made a program for creating a Dungons and Dragons roleplaying character, and after about a day of work, it was working properly. Only for us to find that it did not fully agree with the rules of the game, and it was slightly flawed, so I decided to write a new one. Only problem? Most of the time, while the program should give an output of six numbers, it only gives me anything between two and five. So does one of the great geek spot the problem? Please tell me, it would be a great help! :)
So here's the temporary script: 31 1872
After removing several lines of code a realized that i culd not make sense of whats happening in youre code.
And i do not know the rules of that game so i gave up.
But there is a lot of strange things going on in that pace of code. And there is a lot of dead code in it aswell. No code after line 102 will ever be executet as far as i can see.
But please, Dont use space to indent. Use tab. Or atleast 3+ spaces, Then i culd atleast have used find/replace.
After removing several lines of code a realized that i culd not make sense of whats happening in youre code.
And i do not know the rules of that game so i gave up.
But there is a lot of strange things going on in that pace of code. And there is a lot of dead code in it aswell. No code after line 102 will ever be executet as far as i can see.
But please, Dont use space to indent. Use tab. Or atleast 3+ spaces, Then i culd atleast have used find/replace.
Thank you, Smygis, for your input. However, tabs are not necessary. 4 spaces per indent is what most Python editors use.
bvdet 2,851
Expert Mod 2GB
I cannot tell what your are trying to do with the large number of 'if' statements. If we could find out what your rules are, we might be able to make a suggestion. This will print six numbers: - import random
-
for n in range(6):
-
-
dList = [random.randint(1,6) for _ in range(4)]
-
-
s = random.sample([0,1,2,3], 3)
-
print dList[s[0]]+dList[s[1]]+dList[s[2]]
Hello, I have a problem that's hugely annoying. The other day, me and a friend made a program for creating a Dungons and Dragons roleplaying character, and after about a day of work, it was working properly. Only for us to find that it did not fully agree with the rules of the game, and it was slightly flawed, so I decided to write a new one. Only problem? Most of the time, while the program should give an output of six numbers, it only gives me anything between two and five. So does one of the great geek spot the problem? Please tell me, it would be a great help! :)
Our newest contributor, Smygis, is quite correct. The stucture of you code needs help. We need to help you understand the use of loops and find a more structured way of setting up the rules of the game. However, most Python experts may not know the rules of the game. One thing your code really lacks is comments explaining what it should do. I'll have a brief look, but it will help us help you if you could expain each section of code with comments. Thanks.
I think I see that this -
elif d3<d1 and d3<d2 and d3<d4:
-
print d1+d2+d4
-
break
is intended to print 3 numbers but is written to print the total of them instead. -
elif d3<d1 and d3<d2 and d3<d4:
-
print d1, d2, d4
-
break
This would print 3 numbers and exit the loop.
Thank you, Smygis, for your input. However, tabs are not necessary. 4 spaces per indent is what most Python editors use.
I however prefer tab, But i use a tab with of 4 spaces. And some editors dont use the tab char and simply replace the tab with (4) spaces. What i dont like is one space as indent, It gets hard to read the code in my opinion.
I however prefer tab, But i use a tab with of 4 spaces. And some editors dont use the tab char and simply replace the tab with (4) spaces. What i dont like is one space as indent, It gets hart to read the code in my opinion.
I agree 100%.
Thank you for joining us here at TheScripts.
Here it is, re-indented. Just one command in Boa Constructor!
Here's something with a bit better stucture: - # import needed modules
-
import random
-
-
# define classes
-
class Die(object):
-
"""A class representing a single six-sided die.
-
A die of this class has an optional name.
-
__init__() calls self.role() so creation produces a roled die."""
-
-
def __init__(self, name=""):
-
"""Assign an optional name and role self."""
-
self.name = name
-
self.role()
-
-
def __cmp__(self, other):
-
# overload the compare operation for ==, !=, <, >, etc #
-
return cmp(self.value, other.value)
-
-
def __repr__(self):
-
"""Identify self and report value when printed."""
-
return "My name is %s. My value is %d" %(repr(self.name), self.value)
-
-
-
def role(self):
-
"""Assign a random value between 1 and 6 to the value of this instance."""
-
self.value = random.randint(1,6)
-
-
def get(self):
-
"""Just return the value of this instance."""
-
return self.value
-
-
### define utility functions ###
-
def RoleDice(dice):
-
"""Given a list of die to role, role them.
-
Since lists are mutable, don't actually return the list."""
-
for die in dice:
-
die.role()
-
-
def PrintDice(dice):
-
"""Call each die's __repr__() method with print."""
-
for die in dice:
-
print die
-
-
-
### Define rules ###
-
def AllEqual(dice):
-
"""True or False: Are all the dice of equal value?"""
-
for i in range(len(dice) - 1):
-
if dice[i] != dice[i + 1]:
-
# if any one is NOT EQUAL to another, the answer is false!
-
return False
-
# if it makes it this far, they are all equal
-
return True
-
-
-
### Create objects to work with ###
-
# a list of dice names
-
diceNames = ['First', 'Second', 'Third', 'Forth']
-
-
# a list of named dice
-
dice = [Die(name) for name in diceNames]
-
-
# test compare #
-
dice[0].value = 4
-
dice[1].value = 4
-
print dice[0] == dice[1]
-
-
# test object creation
-
PrintDice(dice)
-
print
-
-
# test utility functions and rules
-
for i in range(5): # run 5 tests
-
RoleDice(dice)
-
PrintDice(dice)
-
print "All dice are %s equal" %["not", ""][AllEqual(dice)]
-
print
with a class and doc strings thrown in for free!
I think I see that this -
elif d3<d1 and d3<d2 and d3<d4:
-
print d1+d2+d4
-
break
is intended to print 3 numbers but is written to print the total of them instead. -
elif d3<d1 and d3<d2 and d3<d4:
-
print d1, d2, d4
-
break
This would print 3 numbers and exit the loop.
No, it is meant to print the sum. I know the code is rather messy. It started out pretty clean, but as my aggression built, my neatness decreased, and whenever it returned the wrong answer, I felt an urge to add more code. With all the "dead code" done, it gives an output of six numbers about half of the time. I'll see what I can do about comments and neatness though.
Here's something with a bit better stucture: - # import needed modules
-
import random
-
-
# define classes
-
class Die(object):
-
"""A class representing a single six-sided die.
-
A die of this class has an optional name.
-
__init__() calls self.role() so creation produces a roled die."""
-
-
def __init__(self, name=""):
-
"""Assign an optional name and role self."""
-
self.name = name
-
self.role()
-
-
def __cmp__(self, other):
-
# overload the compare operation for ==, !=, <, >, etc #
-
return cmp(self.value, other.value)
-
-
def __repr__(self):
-
"""Identify self and report value when printed."""
-
return "My name is %s. My value is %d" %(repr(self.name), self.value)
-
-
-
def role(self):
-
"""Assign a random value between 1 and 6 to the value of this instance."""
-
self.value = random.randint(1,6)
-
-
def get(self):
-
"""Just return the value of this instance."""
-
return self.value
-
-
### define utility functions ###
-
def RoleDice(dice):
-
"""Given a list of die to role, role them.
-
Since lists are mutable, don't actually return the list."""
-
for die in dice:
-
die.role()
-
-
def PrintDice(dice):
-
"""Call each die's __repr__() method with print."""
-
for die in dice:
-
print die
-
-
-
### Define rules ###
-
def AllEqual(dice):
-
"""True or False: Are all the dice of equal value?"""
-
for i in range(len(dice) - 1):
-
if dice[i] != dice[i + 1]:
-
# if any one is NOT EQUAL to another, the answer is false!
-
return False
-
# if it makes it this far, they are all equal
-
return True
-
-
-
### Create objects to work with ###
-
# a list of dice names
-
diceNames = ['First', 'Second', 'Third', 'Forth']
-
-
# a list of named dice
-
dice = [Die(name) for name in diceNames]
-
-
# test compare #
-
dice[0].value = 4
-
dice[1].value = 4
-
print dice[0] == dice[1]
-
-
# test object creation
-
PrintDice(dice)
-
print
-
-
# test utility functions and rules
-
for i in range(5): # run 5 tests
-
RoleDice(dice)
-
PrintDice(dice)
-
print "All dice are %s equal" %["not", ""][AllEqual(dice)]
-
print
with a class and doc strings thrown in for free!
Woah, that is way to complicated for me! I only got into Python some two weeks ago, if that long, and I have no idea what a class is, and definitions are way above me! Please, try to keep it simple!
No, it is meant to print the sum. I know the code is rather messy. It started out pretty clean, but as my aggression built, my neatness decreased, and whenever it returned the wrong answer, I felt an urge to add more code. With all the "dead code" done, it gives an output of six numbers about half of the time. I'll see what I can do about comments and neatness though.
Here's how a list of objects could help you solve the first part: -
-
def AddLowest(nItems, dice):
-
tmpList = sorted(dice)
-
valueList = [die.get() for die in tmpList[0:nItems]]
-
return sum(valueList), tmpList[0:nItems]
-
-
### Create objects to work with ###
-
# a list of dice names
-
diceNames = ['First', 'Second', 'Third', 'Forth', 'Fifth', 'Sixth', 'Seventh']
-
-
# a list of named dice
-
dice = [Die(name) for name in diceNames]
-
-
testResult = AddLowest(3, dice)
-
-
print "The sum of the lowest 3 dice is %d" %testResult[0]
-
print "The lowest 3 dice are:"
-
for die in testResult[1]:
-
print die
-
print
with lots of test code thrown in for free!
Okay, so I'm obviously not allowed to edit the first post... Well, I realize that I failed spectacularly in actually telling you guys what the script should do... Here is what the recipe is supposed to mean, if you were to do it in real life:
Roll four six-sided dice
ignore the smallest
add up the remaining, and note the result
do over until you have got six values
now this seemed rather simple to me at first, but I allways missed something, and when an event occurs that the script does not anticipate, apparently, it just breaks the loop and starts at the wee beginning. The script as it was when I first thought I was finished looked somewhat like this: -
import random
-
-
-
d1="" # Most of the time I have to add these at the top of the script as I go, because otherwise the program gives me the outputs like "d1 not defined", because it didn't bother to roll the dice before checking the program for errors or some such nonsense...
-
d2=""
-
d3=""
-
d4=""
-
-
-
-
for n in range(1,7): # So it does it six times of course
-
-
d1=random.randint(1,6) # Rolls a six sided die
-
d2=random.randint(1,6) # Rolls a second six-sided die
-
d3=random.randint(1,6)
-
d4=random.randint(1,6)
-
-
for n in range(1): # Just to create a loop which can be broken
-
-
if d1<d2 and d1<d3 and d1<d4: # As in "if d1 is smallest"
-
print d2+d3+d4 # Sums them up and prints them
-
break # If I'm not wrong, this breaks the "for n in range(1)" loop, and goes to the second part of the "for n in range(1,7)" loop.
-
-
elif d2<d1 and d2<d3 and d2<d4: # As in if the second die is smallest
-
print d1+d3+d4
-
break
-
-
elif d3<d1 and d3<d2 and d3<d4:
-
print d1+d2+d4
-
break
-
-
elif d4<d1 and d4<d2 and d4<d3:
-
print d1+d2+d3
-
break
-
-
elif d1==d2==d3==d4: # This is the short version, as you might have seen from the previous one. I tried this one first, but I don't think it does what I want it to...
-
print d1+d2+d3 # If they're all alike, it matters not which one I ignore
-
# And since this is the end, I should not have to break a loop, when it only repeats once anyways. Oh well....
-
break
-
-
So there it is, all properly indented and commented. My eyes fail to recognize the fault, but mayhap one of the hawks among you can?
Here's how a list of objects could help you solve the first part: -
-
def AddLowest(nItems, dice):
-
tmpList = sorted(dice)
-
valueList = [die.get() for die in tmpList[0:nItems]]
-
return sum(valueList), tmpList[0:nItems]
-
-
### Create objects to work with ###
-
# a list of dice names
-
diceNames = ['First', 'Second', 'Third', 'Forth', 'Fifth', 'Sixth', 'Seventh']
-
-
# a list of named dice
-
dice = [Die(name) for name in diceNames]
-
-
testResult = AddLowest(3, dice)
-
-
print "The sum of the lowest 3 dice is %d" %testResult[0]
-
print "The lowest 3 dice are:"
-
for die in testResult[1]:
-
print die
-
print
with lots of test code thrown in for free!
Ur... So... I didn't get that. I fell out, like, when you defined AddLowest
Roll four six-sided dice
ignore the smallest
add up the remaining, and note the result
do over until you have got six values
- import random
-
-
for i in range(6):
-
dices = []
-
for i in range(4):
-
dices.append(random.randint(1,6))
-
dices.sort()
-
temp = 0
-
for i in dices[1:]:
-
temp += i
-
print temp
- import random
-
-
for i in range(6):
-
dices = []
-
for i in range(4):
-
dices.append(random.randint(1,6))
-
dices.sort()
-
temp = 0
-
for i in dices[1:]:
-
temp += i
-
print temp
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: - import random
-
-
def role(intList):
-
for i in range(len(intList)):
-
intList[i] = random.randint(1,6)
-
-
def AddLowest(nItems, intList):
-
return sum(sorted(intList)[0:nItems])
-
-
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)
-
Oh, I like the look of that. Now could you explain the temp part? And the dices.sort()...
temp is simply a variable who contains the sum of the three largest values.
dices.sort() sorts the list of dices with the smallest first. Witch then gets "sliced" away. and the rest is added to temp.
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: - import random
-
-
def role(intList):
-
for i in range(len(intList)):
-
intList[i] = random.randint(1,6)
-
-
def AddLowest(nItems, intList):
-
return sum(sorted(intList)[0:nItems])
-
-
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)
-
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: - import random
-
-
def role(intList):
-
for i in range(len(intList)):
-
intList[i] = random.randint(1,6)
-
-
def AddLowest(nItems, intList):
-
return sum(sorted(intList)[0:nItems])
-
-
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)
-
Oh.. Ignore the smallest: - import random
-
-
def role(intList):
-
for i in range(len(intList)):
-
intList[i] = random.randint(1,6)
-
-
def AddLowest(nItems, intList):
-
return sum(sorted(intList)[0:nItems])
-
-
def AddHighest(nItems, intList):
-
return sum(sorted(intList)[-nItems:])
-
-
dice = [i for i in range(4)]
-
role(dice)
-
print dice
-
print AddHighest(3, dice)
-
-
-
nTests = 6
-
for i in range(nTests):
-
role(dice)
-
print AddHighest(3, dice)
-
temp is simply a variable who contains the sum of the three largest values.
dices.sort() sorts the list of dices with the smallest first. Witch then gets "sliced" away. and the rest is added to temp.
Wow, that sounds both neat and easy! Thanks! Now I'll go listen to the other guy to see if he might actually manage to teach me something advanced =P
-
import random
-
-
for i in range(6):
-
dices = []
-
for i in range(4):
-
dices.append(random.randint(1,6))
-
dices.sort()
-
temp = 0
-
for i in dices[1:]:
-
temp += i
-
print temp
-
-
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:] )
-
is a bit shorter tho
and: -
for i in range(6): print reduce(lambda x, y: x+y, sorted([random.randint(1,6) for i in range(4)])[1:] )
-
is only one line. Totaly insane :D
-
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:] )
-
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!
-
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:] )
-
is a bit shorter tho
and: -
for i in range(6): print reduce(lambda x, y: x+y, sorted([random.randint(1,6) for i in range(4)])[1:] )
-
is only one line. Totaly insane :D
Good job, that's creative. Keep posting!
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.
Start look at those when you feel ready for it.
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: -
-
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?
-
-
And that's the hard part all done!
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: -
-
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?
-
-
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.
Other then d2[0} shuld be d2[0]
It's a misspelling on my behalf, and I did not misspell it in the actual program, this is only an approximation. I'm impressed that you noticed it though! I didn't.
It's a misspelling on my behalf, and I did not misspell it in the actual program, this is only an approximation. I'm impressed that you noticed it though! I didn't.
There is much i dont miss.
^^
And you can "overwrite" dice if you want:
Not optmal, but it works.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
1 post
views
Thread by lawentzel |
last post: by
|
101 posts
views
Thread by Elijah Cardon |
last post: by
| |
1 post
views
Thread by Ronald S. Cook |
last post: by
|
5 posts
views
Thread by Steve |
last post: by
| | | | | | | | | | | |