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
Related
This question already has answers here:
multiplication of two arguments in python [duplicate]
(6 answers)
Getting issue while trying to multiply string and integer [duplicate]
(2 answers)
Closed 3 months ago.
I want to output 5 * 2 = 10 but python output is 55!
How do I resolve this problem?
a = 0
b = 2
a = input("a? :") #(get 5 as input)
c = a * b
print (c)
This is my code.
when I input a number it repeat same number I entered two times insterd of showing multipiy it. What do I have to do to solve this?
a is a string,
so it will be
'5'*2='55'
if you want 10, you need to cast a to int.
a=int(input())
here is the link to document
https://docs.python.org/3/library/functions.html#input
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.choice() returns same value at the same second, how does one avoid it?
(3 answers)
random.randint(2, 12) returns same results every time it's run in Python
(5 answers)
Closed 2 years ago.
I am trying to get a random pair of colors from a Python list. Here is my code so far:
from random import *
color_choices = ['#FAACA8', '#DDD6F3', '#21D4FD', '#B721FF', '#08AEEA', '#2AF598', '#FEE140', '#FA709A', '#8EC5FC', '#E0C3FC', '#FBAB7E', '#F7CE68', '#D9AFD9', '#97D9E1', '#FBDA61', '#FF5ACD', '#F76B1C']
shuffle(color_choices)
print(color_choices)
The problem is the it keeps returning the same "random" order on each run of the program
['#21D4FD', '#FBDA61', '#B721FF', '#F76B1C', '#FBAB7E', '#DDD6F3', '#E0C3FC', '#2AF598', '#F7CE68', '#FF5ACD', '#FEE140', '#FA709A', '#8EC5FC', '#08AEEA', '#FAACA8', '#97D9E1', '#D9AFD9']
I ran the program three times. It cannot be a coincidence that the shuffle will return all colors in the exact same order three times.
What am I missing?
Thanks.
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:
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 5 years ago.
lets say I have a list a and a variable b = 1/len(a) but when I display the value of b it gives me 0
Because of integer divided by integer.
Try b=1.0/len(a)