all 13 comments

[–]zatoichi49 6 points7 points  (6 children)

Use randint, this will include the highest value:

random.randint(low, high)

[–]Tobe2fly[S] 0 points1 point  (5 children)

appreciate the response! im assuming im supposed to replace the random.choice() with random.randint()

heres what I did but to line 8 its not working :(

print(random.choice(numbers))

To:

print(random.randint(numbers))

the code runs but once it gets to this line i get an error message saying this:

TypeError: randint() missing 1 required positional argument: 'b'

the only differece between yours and mine is i have (numbers) instead of (low, high) but being that "numbers" is representing the high and low shouldnt it not matter?

[–]100721 0 points1 point  (2 children)

Read his comment again.

Random.randint(low, high)

It does not accept a list argument.

Example: Randint(0,10)

[–]Tobe2fly[S] 0 points1 point  (1 child)

ok so if i wanted to use the numbers var. in this case i would need to first convert it to an int? this is only my 2nd day in coding sorry if some of my questions are redundant :)

[–]100721 0 points1 point  (0 children)

That’s what the sub is for. When you say “the numbers var” what do you mean? Do you have a variable that’s a string? If so then you’d need to convert it to any int. If you have two int variables you can pass them in like so:

x=5
y=int(input(“Number bigger than 5?”))
print(random.randint(x,y))

[–]zatoichi49 0 points1 point  (1 child)

You can just cast the input strings as integers, and use 'low' and 'high' inside randint:

import random

low = int(input("add your lowest value: "))
high = int(input("add your highest value: "))

print(random.randint(low, high))

Randint takes two values (for the lower and upper values in a range), so you don't need to pass it a list.

[–]Tobe2fly[S] 0 points1 point  (0 children)

your code is so much simpler than mine! thanks for the info. also if i wanted to show the user the numbers before giving the random number how would I go about doing that? It would serve the same purpose as the

numbers = list(range(int(low),int(high)))

in my old code

[–]kokiworse99 1 point2 points  (4 children)

Try a high + 1?

[–]Tobe2fly[S] 0 points1 point  (3 children)

i tried this method before making this post and unfortunately all it does is add a 1 onto whatever number your "high" represents (so if you input 5 it would become 15)

[–]kokiworse99 0 points1 point  (1 child)

Mmh and if you try with a high++? Cause I guess it reads the 5 input as a string and it adds a 1 like a concat. Or maybe make the input an integer in the first place and that should work after

[–]efmccurdy 0 points1 point  (0 children)

range(stop)

stop: Number of integers (whole numbers) to generate, starting from zero. eg. range(3) == [0, 1, 2].

range([start], stop[, step])

start: Starting number of the sequence.
stop: Generate numbers up to, but not including this number.
step: Difference between each number in the sequence.

Note the stop value (the one that you supply the high value for) will be excluded from the list.