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
  ValueError: invalid literal for int() with base 10: '' ivinjjunior 6 111 Apr-20-2019, 05:37 PM
Last Post: keames
  help cant do random number nonzzo 1 75 Mar-28-2019, 07:33 PM
Last Post: Yoriz
  Print random musik to wav file Pereskia 2 97 Mar-27-2019, 11:53 AM
Last Post: Pereskia
  random.randrange ravioli2929 14 342 Mar-17-2019, 04:49 PM
Last Post: perfringo
  Question about the Random Module Exsul 1 82 Mar-13-2019, 02:06 AM
Last Post: ichabod801
  Random function sh98765 1 85 Mar-11-2019, 02:08 AM
Last Post: ichabod801
  problems with random number/string PrasadPython 3 133 Mar-01-2019, 06:00 PM
Last Post: ichabod801
  How to import random? 1nc1 1 140 Feb-20-2019, 07:19 AM
Last Post: buran
  Checking input is close to random num Tomington 2 155 Feb-11-2019, 09:44 AM
Last Post: Tomington
  3 random numbers MrGoat 1 197 Jan-15-2019, 06:33 AM
Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)