Random module
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}")
List
list data structure
fruits = [item1, item2]
states_of_america = ["Delaware", "Pennsylvania"]
print(states_of_america[0])
#output
Delaware
the order is determined by the order in the list.
typing zero as the index of the piece of data.
programmers start continuing from zero.
states_of_america = "Pencilvania"
#alter any item inside the list.
#easily using this kind of syntax.
states_of_america.append()
#will add a single item to the end of the list.
append와 extend의 차이
append()
는 object
를 맨뒤에 추가
extend()
는 literable
객체(리스트, 튜플, 딕셔너리 등)의 엘레먼트를 list에 appending 하는 것
using str.split(',')
directly convert it into a list by seperating out the commas using str.split()
str_inp = "Hello, from, AskPython"
op = str_inp.split(",")
print(op)
#output
['Hello', 'from', 'AskPython']
#the words that are divided by this split charater, which is the comma.
Banker Roulette - who will pay the bill?
import random
names_string = input("Give me everybody's names, separated by a comma. ")
names = names_string.split(", ")
#내가 쓴 답
name = random.randint(0, len(names) - 1)
who_pay_bill = names[name]
print(f"{who_pay_bill} is going to buy the meal today!")
#답안
num_items = len(names)
random_choice = random.randint(0, num_items - 1)
person_who_will_pay = names[random_choice]
print(person_who_will_pay + "is going to buy the meal today."
#num_items로 입력하면 error 발생하므로 꼭 num_items - 1로 입력해야 한다.
random.choice()
사용하는 법
a list by writing random.choice, and it will actually pick an item from that list.
person_who_will_pay = random.choice(names)
print(person_who_will_pay + "is going to buy the meal today.")
Dirty dozen foods
dirty_dozen = ["strawberries", "spinach", "kale", "nectarines", "apple", "grapes", "peaches", "cherries", "pears", "tomatoes", "celery", "potatoes"]
fruits = ["strawberries", "nectarines", "apple", "grapes", "peaches", "cherries", "pears"]
vegetables = ["spinach", "kale", "tomatoes", "celery", "potatoes"]
dirty_dozen = [fruits, vegetables]
#dirty_dozen = [["strawberries", "nectarines", "apple", "grapes", "peaches", "cherries", "pears"], ["spinach", "kale", "tomatoes", "celery", "potatoes"]]
Treasure map
row1 = ["⬜️","⬜️","⬜️"]
row2 = ["⬜️","⬜️","⬜️"]
row3 = ["⬜️","⬜️","⬜️"]
map = [row1, row2, row3]
print(f"{row1}\n{row2}\n{row3}")
position = input("Where do you want to put the treasure? ")
first = int(position[0]) - 1
second = int(position[1]) - 1
selected_row = map[vertical - 1]
selected_row[horizonal - 1] = "x"
#map[[int(second)], [int(first)]] = "x" 과 같이 쓸 수도 있음
print(f"{row1}\n{row2}\n{row3}")
Rock Paper Scissors
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?