Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Help with ValueError using ranint from random.
Hi, I'm making a program that generates a given quantity of numbers randomly and prints them, but I don't understand why I get this error:

The code:
from random import randint

# n: normal, generates odd and even numbers.

def n_repeated(q_of_nums, minimum_number, maximum_number):
	"""Generates odd and even numbers, they can be repeated."""
	print("The generated numbers are:")
	for i in list(range(q_of_nums)):
		x = randint(minimum_number, maximum_number)

def n_nonrepeated(q_of_nums, minimum_number, maximum_number):
	"""Generates odd and even numbers, they can't be repeated."""
	printed_numbers = []
	print("The generated numbers are:")
	while printed_numbers < q_of_nums:
		for i in list(range(q_of_nums)):
			x = randint(minimum_number, maximum_number)
			if x in printed_numbers:
				print(x, end=' ')

n_repeated(6, 40, 1)

The error:
The generated numbers are: Traceback (most recent call last): File "", line 27, in <module> n_repeated(6, 40, 1) File "", line 10, in n_repeated x = randint(minimum_number, maximum_number) File "C:\Users\Franc\AppData\Local\Programs\Python\Python37-32\lib\", line 222, in randint return self.randrange(a, b+1) File "C:\Users\Franc\AppData\Local\Programs\Python\Python37-32\lib\", line 200, in randrange raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width)) ValueError: empty range for randrange() (40,2, -38)
Thanks in advance for your help.
print values of minimum_number and maximum_number
just before executing line 10
You're trying to get randint(40, 1). But 40 > 1, so there are no possible numbers randint could return. The first number needs to be less than the second number.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Random selection EMarburg 6 143 Aug-12-2019, 01:15 AM
Last Post: ichabod801
  ValueError: invalid literal for int() with base 10: '' Jay123 7 207 Aug-05-2019, 02:43 PM
Last Post: Jay123
  class random var write to array storzo 5 143 Aug-02-2019, 03:26 PM
Last Post: storzo
  [SOLVED ]ValueError: View function did not return a response Hadad 0 101 Jul-24-2019, 04:01 PM
Last Post: Hadad
  Need to generate random numbers Gateux 8 228 Jul-19-2019, 03:37 PM
Last Post: Man_from_India
  Highlight/Underline a string | ValueError: zero length field name in format searching1 1 198 Jul-01-2019, 03:06 AM
Last Post: metulburr
  Subtract value from random dictionary key:value Tolein 1 136 Jun-12-2019, 02:02 PM
Last Post: perfringo
  ValueError: could not convert string to float michaelbs007 1 908 Jun-08-2019, 08:44 PM
Last Post: micseydel
  ValueError: invalid rectstyle object fen1c5 1 343 Jun-05-2019, 02:51 PM
Last Post: heiner55
  Unique Random ID on Table talius 2 177 May-30-2019, 01:47 PM
Last Post: michalmonday

Forum Jump:

Users browsing this thread: 1 Guest(s)