Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 5 Online
OpenStudy (anonymous):

Rolling the dice! (Homework 2) I simply don't see where my mistake lies. I only get one single number as output, that's it. I tried to do it with the while-loop but it doesn't work nevertheless. What am I doing wrong? Thank you! import random def roll_dice(number,sides): number = int(number) side = int(side) for i in range(1,number+1): randm = random.randint(1,int(sides)) return randm return "That's all!" print roll_dice(3,6)

OpenStudy (microbot):

1.where do you use this? :side = int(side) or is it a typo and it was supposed to be sides? 2.return randm return "That's all!" you only have to use 1 return as the 1st return it sees returns you out of the def function. 3.i do not do the same course as you do...so please could you link me to the homework or explain what are u supposed to do here?cause i dont really see what your code is doing....

OpenStudy (anonymous):

1. yes, it was a typo 2. oh, that makes sense, thank you 3. it is this one (2.4): http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-189-a-gentle-introduction-to-programming-using-python-january-iap-2011/assignments/MIT6_189IAP11_hw2.pdf I've just noticed that I may be in the wrong section. Sorry. But thank you nevertheless, somehow it started to work when I changed the "returns" into the "prints".

OpenStudy (microbot):

well since its a code section is not wrong:)

OpenStudy (microbot):

let me see the problem and get back to you

OpenStudy (microbot):

one last thing. you state: sides = int(sides) and then again: randm = random.randint(1,int(sides)) not needed to make integer again and again

OpenStudy (anonymous):

yes, thank you. I don't quite understand when to use print and when to use return, though. I thought it is necessary to always use return when receiving some output, but it turns out I cannot use it more than once?

OpenStudy (microbot):

well print just prints out smthing or a viariable. return returns smthing you might want to reuse in smting else....or want to do other than just print it out.....in few words that is. you can use return keyword more than once but that would be if there is a possibility that your program will chose that way instead of the other .....and ofc as i said before it will choose the 1st one it reaches... exsample would be: def myfunction(some_variable): if some_variable==True: return True (or smthing else) else: return False (or smthing else) there are many other even more complicated ways to reuse return in your code ...but this should give you the idea.

OpenStudy (anonymous):

got it. thanks!

OpenStudy (microbot):

your welcome:) and merry Xmas!

OpenStudy (anonymous):

Sorry if this is more confusing than helpful, but you don't actually have to return something from a method (if you don't want to!). You can just have it print, and then be done with it. So for example, taking your code. import random def roll_dice(number,sides): number = int(number) side = int(side) for i in range(number): randm = random.randint(1,sides) print randm print "That's all!" roll_dice(3,6) Alternatively, you can return "That's all!" and print roll_dice. There are a lot more ways to do it too, but I just wanted to drive that point home for you. Also, get used to working with indices starting at 0; it will be very helpful as you progress. For example, I replaced: range(1,number+1) with range(number) It looks less awkward, and does the same thing.

OpenStudy (microbot):

i agree on @rapidsnow 's annotation on indices!:)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!