Last active
April 1, 2024 08:34
-
-
Save under-script/fdae11417ef2af990a759a1196632025 to your computer and use it in GitHub Desktop.
Day 4 - Beginner - Randomisation and Python Lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
randomInteger = random.randint(1, 10) | |
print(randomInteger) | |
randomFloat = random.random() * 5 | |
print(randomFloat) | |
love_score = random.randint(1, 100) | |
print(f"Your love score is {love_score}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
random_side = random.randint(0, 1) | |
if random_side == 1: | |
print("Heads") | |
else: | |
print("Tails") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
states_of_america = ["Delaware", "Pennsylvania", "New Jersey", "Georgia", "Connecticut", "Massachusetts", "Maryland", | |
"South Carolina", "New Hampshire", "Virginia", "New York", "North Carolina", "Rhode Island", | |
"Vermont", "Kentucky", "Tennessee", "Ohio", "Louisiana", "Indiana", "Mississippi", "Illinois", | |
"Alabama", "Maine", "Missouri", "Arkansas", "Michigan", "Florida", "Texas", "Iowa", "Wisconsin", | |
"California", "Minnesota", "Oregon", "Kansas", "West Virginia", "Nevada", "Nebraska", "Colorado", | |
"North Dakota", "South Dakota", "Montana", "Washington", "Idaho", "Wyoming", "Utah", "Oklahoma", | |
"New Mexico", "Arizona", "Alaska", "Hawaii"] | |
states_of_america[1] = "Pencilvania" | |
states_of_america.extend(["Angelaland", "jack Bauer Land"]) | |
print(states_of_america) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
names = names_string.split(", ") | |
# Get the total number of items in the list. | |
num_items = len(names) | |
# Generate random numbers between 0 and the last index. | |
random_choice = random.randint(0, num_items - 1) | |
# Pick out a random person from the list of names using the random number. | |
person_who_will_pay = names[random_choice] | |
print(person_who_will_pay + " is going to buy the meal today!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# dirty_dozen = ["Strawberries", "Spinach", "Kale", "Nectarines", "Apples", "Grapes", "Peaches", "Cherries", "Pears", "Tomatoes", "Celery", "Potatoes"] | |
fruits = ["Strawberries", "Nectarines", "Apples", "Grapes", "Peaches", "Cherries", "Pears"] | |
vegetables = ["Spinach", "Kale", "Tomatoes", "Celery", "Potatoes"] | |
dirty_dozen = [fruits, vegetables] | |
print(dirty_dozen) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
line1 = ["⬜️","️⬜️","️⬜️"] | |
line2 = ["⬜️","⬜️","️⬜️"] | |
line3 = ["⬜️️","⬜️️","⬜️️"] | |
map = [line1, line2, line3] | |
print("Hiding your treasure! X marks the spot.") | |
position = input() # Where do you want to put the treasure? | |
# Your code below | |
letter = position[0].lower() | |
abc = ["a", "b", "c"] | |
letter_index = abc.index(letter) | |
number_index = int(position[1]) - 1 | |
map[number_index][letter_index] = "X" | |
print(f"{line1}\n{line2}\n{line3}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
rock = ''' | |
_______ | |
---' ____) | |
(_____) | |
(_____) | |
(____) | |
---.__(___) | |
''' | |
paper = ''' | |
_______ | |
---' ____)____ | |
______) | |
_______) | |
_______) | |
---.__________) | |
''' | |
scissors = ''' | |
_______ | |
---' ____)____ | |
______) | |
__________) | |
(____) | |
---.__(___) | |
''' | |
game_images = [rock, paper, scissors] | |
user_choice = int(input("What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors.\n")) | |
print(game_images[user_choice]) | |
computer_choice = random.randint(0, 2) | |
print("Computer chose:") | |
print(game_images[computer_choice]) | |
if user_choice >= 3 or user_choice < 0: | |
print("You typed an invalid number, you lose!") | |
elif user_choice == 0 and computer_choice == 2: | |
print("You win!") | |
elif computer_choice == 0 and user_choice == 2: | |
print("You lose") | |
elif computer_choice > user_choice: | |
print("You lose") | |
elif user_choice > computer_choice: | |
print("You win!") | |
elif computer_choice == user_choice: | |
print("It's a draw") | |
####### Debugging challenge: ######### | |
#Try running this code and type 5. | |
#It will give you an IndexError and point to line 32 as the issue. | |
#But on line 38 we are trying to prevent a crash by detecting | |
#any numbers great than or equal to 3 or less than 0. | |
#So what's going on? | |
#Can you debug the code and fix it? | |
#Solution: https://repl.it/@appbrewery/rock-paper-scissors-debugged-end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment