0
Q:

how to use random in python

# generate random integer values
from random import randint

value = randint(0, 10)
print(value)
35
import random
print(random.randint(3, 7)) #Prints a random number between 3 and 7
array = [cars, bananas, jet]
print(random.choice(array)) #Prints one of the values in the array at random
3
# imports random
import random
# randint generates a random integar between the first parameter and the second
print(random.randint(1, 100))
34
import random

random.randint(lower, upper)
6
# imports random
import random
# randint generates a random integar between the first parameter and the second
print(random.randint(1, 100))
# random generates a random real number in the interval [0, 1)
print(random.random())
19
import random
5
import random
a = (random.randint(bottom value, top value)
3
print(random.randint(1, 100))
print(random.random())
3
random.choice
1
#Using Random choice python
#Running Race game
import time
import random
gamelist = ['You lose the race', 'You win the race']
Player = input('')
print(' Hi ' + Player + ' , welcome to the running race game')
time.sleep(2)
print('3')
time.sleep(1)
print('2')
time.sleep(1)
print('1')
time.sleep(2)
print(random.choice(gamelist))
0
# I know somebody else has made a similar thing from mine.
# Just letting you know that I didn't mean to copy his idea for this code.
# If you saw this, I recommend check the other answers out, too.
# Hope you guys understand...
from random import randint

# Prints a random number in between 1 and 1000
print(f"Here is a random number: {randint(1, 1000)}")
1
enota = input("V kateri enoti boš podal temperaturo? Vnesi 'C' ali 'F': ")
stopinje = int(input("Število stopinj: "))

if enota != "C" and enota != "F":
        print("Vnesi ustrezno enoto.")
else:
    if enota == "C":
        odg = int(round((9 * stopinje) / 5 + 32))
nova_enota = "Fahrenheit"
    elif enota == "F":
        odg = int(round((stopinje - 32) * 5 / 9))
nova_enota = "Celzija"
print("Temperatura v stopinjah", nova_enota, "je", odg, "stopinj.")
0

New to Communities?

Join the community