Python Forum
How do you add the results together each time a function is called?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you add the results together each time a function is called?
#2
First, a list would be a better way to store the results. If you have counts = [0] * 11, then you can avoid the if/elif chain. I would also eliminate looping through twice:

for num in range(1000):
    counts[random.randint(1, 10)] += 1
Then a simple list comprehension gets the percentages:

percentages = [count / 1000 for count in counts]
Note that your ability to use list_length in a function is not inexplicable. If Python can't find the value in the function scope, it will check the module/global scope for it. That is, as long as you haven't done an assignment to that name, which flags the name as local scope to Python.

Then you can check that sum(percentages) == 1. I expect it will be wrong due to the minor inaccuracies of floating point math, but it should be close.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: How do you add the results together each time a function is called? - by ichabod801 - Jul-31-2019, 01:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiple variable inputs when only one is called for ChrisDall 2 517 Oct-20-2023, 07:43 PM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,377 Aug-06-2023, 02:29 PM
Last Post: deanhystad
Bug New to coding, Using the zip() function to create Diret and getting weird results Shagamatula 6 1,498 Apr-09-2023, 02:35 PM
Last Post: Shagamatula
  Couldn't install a go-game called dlgo Nomamesse 14 3,233 Jan-05-2023, 06:38 PM
Last Post: Nomamesse
  how can a function find the name by which it is called? Skaperen 18 3,570 Aug-24-2022, 04:52 PM
Last Post: Skaperen
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,231 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  time function does not work tester_V 4 3,105 Oct-17-2021, 05:48 PM
Last Post: tester_V
  How to print results of asyncio websockets at the same time? codingmonster 0 1,784 Jun-04-2021, 01:48 PM
Last Post: codingmonster
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,319 May-20-2021, 01:52 PM
Last Post: DeaD_EyE
  Can you end the Time.sleep function boier96 9 9,608 Jan-16-2021, 10:09 PM
Last Post: Serafim

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020