[Tutor] Help with a funcion
Cameron Simpson
cs at cskk.id.au
Mon Dec 30 18:12:28 EST 2019
On 30Dec2019 13:09, Laura Schramm <laura.schramm92 at gmail.com> wrote:
>I was hoping you could help me out creating a function.
We can, but we won't actually write it for you. We will offer
suggestions, but it works is best when you come with code you've written
which doesn't work and explain what it currently does and what you
actually want it to do.
Anyway, discussion and suggestions below:
>My professor has requested that I create a function that simulates m throws
>of a dice with n sides. The function should return a list of tuples with
>(value_side, percentage) for every side. The percentages should be
>expressed as a chain of text with a decimal and "%".
>
>For example: For 1000 throws of a dice with 4 sides the answer should look
>like
>[(1, 25.3%), (2, 25.8%), (3, 25.0%), (4, 24.5%)]
This looks much like the output of Python's repr() function, except that
the percentages will be strings, so:
[(1, '25.3%'), (2, '25.8%'), (3, '25.0%'), (4, '24.5%')]
For example, you can check the result of your programme like this:
result = throws_of_dice(4, 100)
print(repr(result))
which woould print a line like the above.
Python's interacive prompt doesthe second line for you if you're working
that way:
>>> throws_of_dice(4, 100)
[(1, '25.3%'), (2, '25.8%'), (3, '25.0%'), (4, '24.5%')]
>>>
(Note, no assignment to "result" there; that would omit the output.)
>I am given the prompt
>def throws_of_dice(n_sides, n_throws)
This is the start of your function definition. It will look more like
this when written:
def throws_of_dice(n_sides, n_throws):
... throw some dice many times, count the results ...
return the list ofresults
You need to do a few things in your function:
- write a short "for" loop which runs n_throws times (see the range()
function for an easy way to do this)
https://docs.python.org/3/reference/compound_stmts.html#the-for-statement
There's even an example for-loop in the above using range().
- write a little line of code to simulate a throw, by computing a random
value from 1 to n_sides; see the "random" module, which has various
functions including one to compute a random value like that
https://docs.python.org/3/library/random.html#module-random
- keep an association of the count of times each side shows up; the
simplest is a dict keyed on the "side" (a number from 1 to n_sides),
which you could prefill with 0 counts before the main loop runs; on
each loop iteration, add one to the appropriate count
- collate those results as a list of tuples for return; this involves
iterating over the dictionary items (which gets you the key (side) and
value (count)), which you can use to prepare a single tuple for that
side. Start by ignoring the "count as a percentage" thing and just put
in the count as the second tuple element; append each tuple to the
list
- when that returns a good result for you (like
[(1,3),(2,2),(3,4),(4,3)]), _then_ change the code to emit percentages
instead of the count. That will require a "total_count" value in order
to compute the percentage, and to use formatted strings to express the
percentage
Sketch out a function doing some or all of the above,and when you get
stalled with a problem you can't solve, come back with the current code
of your function, its output, and describe what you're not accomplishing
so that we can help further.
Finally: put in lots of print() calls in your function while writing it:
print(repr(something))
This will let you see the state of things in your function as it runs,
which is a great aid to figuring out what may be going right or wrong.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Tutor
mailing list