I’ve been through some answers to similar questions on stackoverflow, whereas I appears to be facing a quite delicate situation wherein some of them won’t work. below is my code:
import random from datetime import datetime array = ['a', 'b', 'c', 'd', 'e', 'f'] def log(): print(random.choice(array)) characters_to_remove = ".-:" now = str(datetime.now()).replace(" ", "") for character in characters_to_remove: now = now.replace(character, "") # print(now) time = now time = int(time) # print(time) paramTime = time % 535 # print(paramTime) # param = int(str(paramTime)[:2]) param = int(str(paramTime)[:1]) # print(param) def repeat(times, func): for i in range(param): func() print(repeat(param, log)) # print(str(repeat(param, log)).split("")) # print('----------')
as you can see, the for loop is used in order to call a function (for multiple times):
def repeat(param, func): # param stands for the number of times the function is to be called for i in range(param): func()
now I want to convert my outputs as a list and ergo be able to get the most commonly outputted value… what do I do?
There is no good way to convert the output of a
print() into values that can be collected.
You just need functions which return values and a loop which collects them:
import random array = ['a', 'b', 'c', 'd', 'e', 'f'] def choose(): return random.choice(array) def repeat(param, func): # param stands for the number of times the function is to be called return [func() for _ in range(param)] # ... # other parts elided print(repeat(param, choose))
The expected output is a
list which you can examine or manipulate further.
Also, see this answer for a way to get the most popular value.
You can use it like this:
def get_max(l): return max(l, key=l.count) print(get_max(repeat(param,log)))