This is a 100 Days challenge to learn a new language (Python). 100 Days of Code - The Complete Python Pro Bootcamp
I will post some notes to motivate myself to finish this challenge.
Random Module
Ex:
# import random module
import random
# Returns a random integer between a and b (both inclusive)
random_int = random.randint(1, 100)
print(random_int)
# Returns the next random floating point number between [0.0 to 1.0)
random_float = random.random()
print(random_float)
Result:
9
0.7455953290494418NOTE: What is pseudorandom number generators? Reference
List
# List
fruits = ["banana", "apple"]
print(fruits)
# How to point out specific item from list
print(fruits[1])
# How to add one item into list
fruits.append("cherry")
print(fruits)
# How to add multiple items into list
fruits.extend(["pear", "kiwi"])
print(fruits)
Result:
['banana', 'apple']
apple
['banana', 'apple', 'cherry']
['banana', 'apple', 'cherry', 'pear', 'kiwi']Nested List
Items in a list have the similar aspect, and that is why they are put into a list
But with more detail information, we can categorize them into another sub list
Ex:
# regular list
car = ['m3', 'x3', 'x5', 'cayenne', 'taycan', '911']
print(car)
# nested list
bmw = ['m3', 'x3', 'x5']
porsche = ['cayenne', 'taycan', '911']
car = [bmw, porsche]
print(car)
Result:
['m3', 'x3', 'x5', 'cayenne', 'taycan', '911']
[['m3', 'x3', 'x5'], ['cayenne', 'taycan', '911']]
Challenge - Coin Toss Game
Ex:
# Remember to use the random module
import random
# By default the random number generator uses the current system time.
# Use the seed() method to customize the start number of
# the random number generator.
# random.seed(100)
# generate a random integer from o or 1
rand_int = random.randint(0, 1)
if rand_int == 1:
print("Heads")
else:
print("Tails")
Result:
Heads
Challenge - Who Pay Bill?
Ex:
import random
names_string = input("Give me everybody's names, separated by a comma. ")
names = names_string.split(", ")
# Don't use 'choice' function from random module for this exercise
random_index = random.randint(0, len(names)-1)
print(f"{names[random_index]} is going to buy the meal today!")
Result:
Give me everybody's names, separated by a comma.
Frank, Jake, Mary, Demo, Tester
Frank is going to buy the meal today!
Challenge - Rock Paper Scissors Game
Ex:
import random
rock = '''
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
'''
paper = '''
_______
---' ____)____
______)
_______)
_______)
---.__________)
'''
scissors = '''
_______
---' ____)____
______)
__________)
(____)
---.__(___)
'''
# list of options
options = [rock, paper, scissors]
# Get User Choice
user_choice = int(
input("What do you choose? Type 0 for Rock, 1 for Paper or
2 for Scissors."))
print(options[user_choice])
# Get Computer Choice
computer_choice = random.randint(0, len(options)-1)
print("Computer chose:")
print(options[computer_choice])
if user_choice == computer_choice:
print("It's a draw")
elif user_choice == 0 and computer_choice == 2:
print("You win!")
elif user_choice == 2 and computer_choice == 0:
print("You lose")
elif computer_choice > user_choice:
print("You lose")
else:
print("You win!")
Result:
What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors.1
_______
---' ____)____
______)
_______)
_______)
---.__________)
Computer chose:
_______
---' ____)____
______)
__________)
(____)
---.__(___)
You lose
No comments:
Post a Comment