Bottom Page

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 >>>
ichabod801 wrote Nov-17-2019, 06:59 PM:
Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.
Quote
#2
FYI,
I am not getting any traceback.
Quote
#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

Quote
#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
Quote
#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

Quote
#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}")
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Finding an element in a 1d list in a 2d array lionrocker221 0 148 Jun-27-2020, 04:50 PM
Last Post: lionrocker221
  Help with a random number generator dpcalder 2 96 Jun-20-2020, 03:50 AM
Last Post: buran
  Make an array of string number in a List polantas 5 208 May-27-2020, 07:18 AM
Last Post: buran
  Factorial Code is not working when the given number is very long integer Raj_Kumar 2 249 Mar-31-2020, 06:40 PM
Last Post: deanhystad
  Generate only one random number for many tries Bhavika 2 157 Mar-29-2020, 12:12 PM
Last Post: Bhavika
  adding elements to a list that are more than a specific number Olavv 2 246 Mar-19-2020, 06:05 PM
Last Post: Olavv
  Adding elements to a list by number Olavv 4 318 Mar-08-2020, 11:16 AM
Last Post: ndc85430
  List of n random elements medatib531 5 456 Mar-07-2020, 07:09 PM
Last Post: jefsummers
  counting items in a list of number combinations Dixon 2 265 Feb-19-2020, 07:06 PM
Last Post: Dixon
  random.uniform is not working correctly dron4ik86 9 400 Feb-06-2020, 03:04 PM
Last Post: dron4ik86

Forum Jump:


Users browsing this thread: 1 Guest(s)