This question already has answers here:
How can I randomly select an item from a list?
(17 answers)
Closed 5 years ago.
I am making a mini version of Russian Rullet.
I have a code which gives me a random number from 1 - 36 but I also need to ask my program to randomly give either Red or Black?
from random import randint
print(randint(1,36))
print (randint ('Red', 'Black'))
I wouldn't recommend this, but
from random import randint
print(["Red","Black"][randint(0,1)])
Check this
a = randint(0,1)
color = 'Red' if a == 1 else color == 'Black'
print(color)
Related
This question already has answers here:
How can I print multiple things on the same line, one at a time?
(18 answers)
How to print without a newline or space
(26 answers)
How can I print variable and string on same line in Python? [duplicate]
(18 answers)
Closed 1 year ago.
Not sure if this is asked before but: How do you print "searching for x" where "x" is a random integer? I have my code below:
from random import randint
numbers = []
random = randint(1,50)
for i in range(0,10):
numbers.append(randint(1,50))
for j in range(len(numbers)):
print('searching for')
print(random)
print('in')
print(numbers)
break
And this is what happens but I want "searching for __ in [list]" on the same line. Is there a way to do it?
Thanks in advance!
try this:
print(f'searching for {random} in {numbers}')
it requires python 3.6 or up and it is called an f-string
for n in numbers:
print("Searching for {} in {}".format(n, numbers))
Does that answer what you want to do?
This question already has answers here:
Random number between 0 and 1? [duplicate]
(7 answers)
Closed 2 years ago.
I want to define a variable s which can only take real values between 0 and 1 in python, how to write the code for it.
Use Random library:
import random
s = random.random() # the default range is [0,1)
This question already has answers here:
Generate random integers between 0 and 9
(22 answers)
Closed 3 years ago.
How can I get a random integer?
I've tried to get()
but it does not work.
def get():
a = randomNumber()
return a
print get()
You can use the random module
import random
number = random.randint(1,5)
print number
This question already has answers here:
Generate 'n' unique random numbers within a range [duplicate]
(4 answers)
Closed 7 years ago.
I have a list that looks like this:
l = [random.randint(0,9),random.randint(0,9),random.randint(0,9),random.randint(0,9)]
but if it outputs something like this[9,0,5,5] what can I do replace the repeating integer?
If the goal is to get four unique items in random order, let Python do the work for you:
l = random.sample(range(10), 4)
random.sample is intended specifically for "random sampling without replacement", which appears to be the goal of your code.
This question already has answers here:
How can I randomly select an item from a list?
(17 answers)
Closed 8 years ago.
I'm trying to make a quiz but I want to know how to make a random word generator.
The 4 words I need to be in this generator are: add, minus, divide and times.
question = random.randint(1,4)
if (question==1):
elif (question==2):
elif (question==3):
elif (question==4):
I'm using this for now but I would prefer for it to be words
Simply use random.choice. Using it, you don't need to index.
>>> import random
>>> random.choice(['add', 'minus', 'divide', 'times'])
'add'
>>> random.choice(['add', 'minus', 'divide', 'times'])
'times'