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
  Checking input is close to random num Tomington 2 70 Feb-11-2019, 09:44 AM
Last Post: Tomington
  ValueError: could not convert string to float Prince_Bhatia 2 324 Jan-26-2019, 02:37 PM
Last Post: perfringo
  3 random numbers MrGoat 1 118 Jan-15-2019, 06:33 AM
Last Post: Larz60+
  from 50 random numbers printing only the sqrt(...)==0 AFKManager 1 121 Jan-12-2019, 08:16 PM
Last Post: ichabod801
  ValueError - arg is an empty sequence jojotte 2 246 Dec-31-2018, 10:07 AM
Last Post: jojotte
  random number anasa 2 214 Nov-26-2018, 08:56 PM
Last Post: Excelsiscelia
  Animating Random Walk Excelsiscelia 2 279 Nov-24-2018, 08:53 AM
Last Post: Excelsiscelia
  Random Expression Creator SheeppOSU 2 219 Nov-01-2018, 11:08 PM
Last Post: wavic
  SARIMAX ValueError: xnames and params do not have the same length preetha8715 2 220 Oct-26-2018, 08:16 AM
Last Post: buran
  Random module Python 3.6 daryl_uk 1 217 Oct-25-2018, 12:09 AM
Last Post: micseydel

Forum Jump:

Users browsing this thread: 1 Guest(s)