'Python - saving the for loop's result
result = [ ]
cents = [25, 10, 5, 1]
number = 141
for cent in cents:
if number >= cent:
helper = number // cent
number = number - (helper*cent)
result.append(helper)
print(result)
I know this might be an easy question, but how do I save the results in such a way that the program output would be a string (example: "25 * 5, 10 * 1, 5 * 1, 1 * 1") or a list [25 * 5, 10 * 1, 5 * 1, 1 * 1]
Solution 1:[1]
Simply change your append call to:
result.append(f'{cent} * {helper}')
or
result.append(str(cent) + ' * ' + str(helper)).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | StBlaize |
