Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Help with ValueError using ranint from random. FWendeburg Programmer named Tim Posts: 14 Threads: 10 Joined: Jan 2019 Reputation: 0 Likes received: 0 #1 Feb-14-2019, 02:49 AM 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) return 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: continue else: print(x, end=' ') printed_numbers.append(x) n_repeated(6, 40, 1) ``` The error: ``````Error: The generated numbers are: Traceback (most recent call last): File "test.py", line 27, in n_repeated(6, 40, 1) File "test.py", line 10, in n_repeated x = randint(minimum_number, maximum_number) File "C:\Users\Franc\AppData\Local\Programs\Python\Python37-32\lib\random.py", line 222, in randint return self.randrange(a, b+1) File "C:\Users\Franc\AppData\Local\Programs\Python\Python37-32\lib\random.py", 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. Larz60+ aetate et sapientia Posts: 7,396 Threads: 287 Joined: Sep 2016 Reputation: 264 Likes received: 1034 #2 Feb-14-2019, 03:00 AM print values of minimum_number and maximum_number just before executing line 10 ichabod801 Bunny Rabbit Posts: 3,513 Threads: 73 Joined: Sep 2016 Reputation: 194 Likes received: 1069 #3 Feb-14-2019, 04:22 AM 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 - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures « Next Oldest | Next Newest »

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)