1

I have made this code below :

a = list(input("Enter some numbers"))
b = list(input("Enter some numbers"))

c = a + b

def PuzzleKey():
    """
    the variable c sums a and b, and if their sum is equal to 2O20, we multiply them
    """
    if c == 2020: 
        return a * b

print(PuzzleKey())

Instead of lines 1-2, I need to generate random values instead of creating ones myself, how can I do that?

Gandoulf
  • 13
  • 7
  • 1
    How can adding two *lists* produce an integer (._.)? Do you mean `c = sum(a + b)`? – Nishant Dec 03 '20 at 08:51
  • Dupe of https://stackoverflow.com/questions/3996904/generate-random-integers-between-0-and-9/3996930#3996930? – Maarten Bodewes Dec 03 '20 at 09:00
  • Is your input requiring more than one `int` for `a` and `b`? – etch_45 Dec 03 '20 at 09:34
  • @MaartenBodewes - The post you've linked is asking about a single random value. And this one is asking for multiple inputs for each variable (a, b) since the wording is "Enter some numbers" and it's stored in a list. – etch_45 Dec 03 '20 at 09:51

3 Answers3

2

The randrange function will return you a random integer between zero and your argument:

from random import randrange

a = randrange(1000)
b = randrange(1000)

Also, you can pass specific arguments to this function:

randrange(start, stop[, step])
Alex
  • 798
  • 1
  • 8
  • 21
  • 1
    I feel seriously dumb after finding that answer, my question isn't worth to ask in stack overflow, I think – Gandoulf Dec 03 '20 at 08:49
  • does it send the output already or I have to run this program several times until it sends an output? – Gandoulf Dec 03 '20 at 08:58
  • This solution binds the value of `randrange()` once it it is called. Also, the question ask for a list of integers it appears. – etch_45 Dec 03 '20 at 09:30
1
import random
x=random.randint(lowerNumber,higherNumber)
OnlyTrueJames
  • 47
  • 1
  • 8
  • This solution binds the value of randint once it is called. Also, the question ask for a list of integers. If this is put into a list multiple times, it will be the same value. – etch_45 Dec 03 '20 at 09:29
0

The random value should be defined in a separate function. This way it will be able to be called on easily throughout the program.

import random

def rand_int(lower, upper):
     return random.randint(lower, upper)

Output -

>>> rand_int(1,10)
4
>>> rand_int(1,10)
3

Incorporating it with your script, for example -

>>> a = [rand_int(1,10), rand_int(1,11), rand_int(1,17)]
>>> a
[6, 10, 14]  
etch_45
  • 792
  • 1
  • 6
  • 21