randint(1, 10) returns integer btw 1 and 10
import random
random_integer = random.randint(1, 10)
print(random_integer)
#0.0000 - 4.99999 => *5
random_float = random.random()
print(random.random())
Random is a module. Built in functionality. Also, I can create a module.py for my own use. I can use it just by importing it.
Exercise 4.1 Heads of Tails
Seed for python random module is current timestamp. However, we can change it into anything we want.
#Remember to use the random module 👇
import random
# 🚨 Don't change the code below 👇
test_seed = int(input("Create a seed number: "))
random.seed(test_seed)
# 🚨 Don't change the code above 👆
#Write your code below this line 👇
coin_toss = random.randint(0, 1)
if coin_toss == 0:
print("Tails")
else:
print("Heads")
Data structure. Variable is for one piece of data. But list is for groupped data.
List has an order.
first itme starts with "0". '-1' is last one.
To alter the existing data in the list, simply
list[1] = changedtothis
list.append("addthis")
list.extend : adding a list in the end of the existing list.
Exercise 4.2 Who's paying?
List index for starting from the end: 0, len()-1
.split()
import random
# 🚨 Don't change the code below 👇
test_seed = int(input("Create a seed number: "))
random.seed(test_seed)
# Split string method
namesAsCSV = input("Give me everybody's names, seperated by a comma. ")
names = namesAsCSV.split(", ")
# 🚨 Don't change the code above 👆
#Write your code below this line 👇
#name = random.choice(names)
#print(f"{name} is going to buy the meal today!")
num_items = len(names)
random_choice = random.randint(0, num_items - 1)
person_pay = names[random_choice]
print(person_pay)
map[first][second] = "X"
to approach the index in the nested lists
Bigger list to small ones..
# 🚨 Don't change the code below 👇
row1 = ["⬜️","⬜️","⬜️"]
row2 = ["⬜️","⬜️","⬜️"]
row3 = ["⬜️","⬜️","⬜️"]
map = [row1, row2, row3]
print(f"{row1}\n{row2}\n{row3}")
position = input("Where do you want to put the treasure? ")
# 🚨 Don't change the code above 👆
#Write your code below this row 👇
row = int(position[0]) - 1
column = int(position[1]) - 1
map[row][column] = "X"
#Write your code above this row 👆
# 🚨 Don't change the code below 👇
print(f"{row1}\n{row2}\n{row3}")
Project. Rock Paper Scissor
rock = '''
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
'''
paper = '''
_______
---' ____)____
______)
_______)
_______)
---.__________)
'''
scissors = '''
_______
---' ____)____
______)
__________)
(____)
---.__(___)
'''
import random
user = input("What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors. \n")
list = [rock, paper, scissors]
user_choice = int(user)
print(list[user_choice])
num = [0, 1, 2]
computer_choice = random.choice(num)
computer = list[int(computer_choice)]
print(f"Computer Chose:{computer}")
#my code for "If part".
if user_choice == 0:
if computer_choice == 0:
print("same")
elif computer_choice == 1:
print("you lose")
else:
print("you win")
elif user_choice == 1:
if computer_choice == 0:
print("you win")
elif computer_choice == 1:
print("same")
else:
print("you lose")
else:
if computer_choice == 0:
print("you lose")
elif computer_choice == 1:
print("you win")
else:
print("same")
Programming is an open book exam!