Python Forum
Finding MINIMUM number in a random list is not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding MINIMUM number in a random list is not working
#1
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 >>>
Reply
#2
FYI,
I am not getting any traceback.
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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
Reply
#5
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
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}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sample random, unique string pairs from a list without repetitions walterwhite 1 401 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  Error is finding mean of a list PythonBoy 4 844 Sep-11-2023, 02:38 PM
Last Post: PythonBoy
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,015 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Finding combinations of list of items (30 or so) LynnS 1 838 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  List of random numbers astral_travel 17 2,534 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,438 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Finding the price based on industry and number of transactions chandramouliarun 0 897 Jul-26-2022, 07:36 PM
Last Post: chandramouliarun
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,765 Jul-01-2022, 01:23 PM
Last Post: deanhystad
Question Finding string in list item jesse68 8 1,801 Jun-30-2022, 08:27 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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