Hello,
I am trying to display the minimum number in a randomly created list but I am getting noting printed for the min function. I have my codes and display below. Any help is highly appreciated.
Here are the codes:
import random
random_numbers = []
average = 0
total = 0
for i in range(3):
x = int((random.randint(0, 9)))
random_numbers.append(x)
total = sum(random_numbers)
average = float(total / 3)
random_numbers = ', '.join(map(str, random_numbers))
print(random_numbers)
print("Low:", min(random_numbers))
print("High:", max(random_numbers))
print("average:", format(average,'.2f'))
print("Total:", total)
Here is the display:
Output:
>>>
9, 3, 2
Low:
High: 9
average: 4.67
Total: 14
>>>
FYI,
I am not getting any traceback.
On line 13 you convert random_numbers to a string. Min then finds the lowest character (the character with the lowest ord()). That happens to be a space, so it looks like nothing prints.
Use a different variable name for the assignment on line 13 and the print on line 14.
Thank you very much for your help. It is working as below now.
import random
random_numbers = []
average = 0
total = 0
for i in range(3):
x = int((random.randint(0, 9)))
random_numbers.append(x)
total = sum(random_numbers)
average = float(total / 3)
random_numbers_comma = ', '.join(map(str, random_numbers))
print(random_numbers_comma)
print("Low:", min(random_numbers))
print("High:", max(random_numbers))
print("average:", format(average,'.2f'))
print("Total:", total)
Output:
9, 5, 5
Low: 5
High: 9
average: 6.33
Total: 19
A few seconds before your reply I figured a work around as well. Below is the work around.
import random
random_numbers = []
average = 0
total = 0
for i in range(3):
x = int((random.randint(0, 9)))
random_numbers.append(x)
total = sum(random_numbers)
average = float(total / 3)
list.sort(random_numbers)
random_numbers = ', '.join(map(str, random_numbers))
print(random_numbers)
print("Low:", random_numbers[:1])
print("High:", max(random_numbers))
print("average:", format(average,'.2f'))
print("Total:", total)
Output:
2, 3, 7
Low: 2
High: 7
average: 4.00
Total: 12
Okay, but your work around finds the first digit of the lowest number. While that works because they are all single digit numbers, it won't work with larger numbers.
I refactored your initial code a bit:
import random
# these three lines are obsolete as variables doesn´t need to be "initialized"
# random_numbers = []
# average = 0
# total = 0
# this for loop can be simplified
# for i in range(3):
# x = int((random.randint(0, 9)))
# random_numbers.append(x)
# total = sum(random_numbers) # why are you calculating the total value each loop ???
# average = float(total / 3) # why are you calculating the average each loop ???
amount = 11
random_numbers = [random.randint(0, 9) for x in range(amount)]
print(f"Numbers: {random_numbers}")
print(f"Low: {min(random_numbers)}")
print(f"High: {max(random_numbers)}")
total = sum(random_numbers)
print(f"Total: {total}")
print(f"Average: {total/amount:.2f}")