This is a question close to some others I have found but they don't help me so I'm asking specifically for me and my purpose this time.
I'm coding for a bot that is supposed to ask the user for max and min in a range, then generating ten random numbers within that range. When validating I'm told both random
and i
are unused variables. I don't really get why. I believed random.randint
was supposed to be a built-in function and as far as i
is concerned I really don't know what to believe. This is what I've got so far.
def RandomNumbers():
"""
Asking the user for a min and max, then printing ten random numbers between min and max.
"""
print("Give me two numbers, a min and a max")
a = input("Select min. ")
b = input("Select max. ")
for i in range(10):
number = random.randint(a, b)
print(str(number)+ str(","), end="")
I'll be very happy for every piece of advice I can get to complete my task. Thank you in advance!
No. random.randint
is not a builtin function. You'll have to import the random
module to use the function.
On another note, i
was evidently not used in the loop, so you'll conventionally use the underscore _
in place of i
import random
numbers = []
for _ in range(10):
numbers.append(random.randint(a, b))
You'll also notice I have used a list to store all the values from each iteration. In that way, you don't throw away the values from previous iterations.
In case you're not already familiar with lists, you can check out the docs:
Data structures
Lists:
The items of a list are arbitrary Python objects. Lists are formed by placing a comma-separated list of expressions in square brackets
On a final note, to print the items from your list, you can use the str.join
method, but not after the items in your list have been converted from integers to strings:
output = ', '.join([str(num) for num in numbers])
print(output)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With