티스토리 뷰

Programming/Python

Day 4. Random , List , Sequence

우주아줌마 2022. 8. 16. 18:51
728x90
반응형
import random

rock = '''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

paper = '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
'''

scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''

#Write your code below this line 👇
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"))
if user_choice >=3 or user_choice < 0:
  print("you typed an invalid number, you lose!")
else:
  print("You choice :\n")
  print(game_images[user_choice])

computer_choice = random.randint(0,2)
print("computer chose :\n")
print(game_images[computer_choice])



if 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 computer_choice == user_choice:
  print("It's a draw")

Day 4, 랜덤 함수 , 리스트 , 배열

Python 부트 캠프 : 100개 프로젝트로 Python 공부!

 

# # random module
# # list

# a = 3
# # b = 'Hello'
# # print(a+1)
# # print(b)

# # items = ["item1", "item2","item3"]

# # print(items[0])
# # # count(items)
# # print(type(items))

# # print(len(items))

# # Split String method , rulet game (random,random.choice)
# import random

# name_string = input("Give me everybody's name, seperated by a comm ")
# names = name_string.split(",")

# print(names)
# names_len = len(names)
# print(names_len)

# random_names = random.randint(0,names_len - 1)
# print(random_names)

# choice = names[random_names]
# print(choice)

# print(random.choice(names))


# 가위 바위 보 게임 만들기

import random

rock = '''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

paper = '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
'''

scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''

#Write your code below this line 👇

ran_user = random.randint(0,2)
ran_com = random.randint(0,2)

if ran_user == 0:
  user = 0
  print("user !!! " , rock)
elif ran_user == 1:
  user = 1
  # user1 = paper
  print("user !!! " , paper)
else:
  user = 2
  # user2 = scissors
  print("user !!! " , scissors)

if ran_com == 0:
  com = 0
  # com0 = rock
  print("com !!! " , rock)
elif ran_com == 1:
  com = 1
  # com1 = paper
  print("com !!! " , paper)
else:
  com = 2
  # com2 = scissors
  print("com !!! " , scissors)

if user == com:
  print
  print("same!!! ")
  if user == 0 and com == 1:
    print(rock)
    print(paper)
    print("com wIN")
    if user == 1 and com == 0:
      print(paper)
      print(rock)
      print("com win")
      if user == 1 and com == 2:
        print(paper)
        print(scissors)
        print("com win")
      else:
        print(paper)
        print(scissors)
        print("user win")

까지 하다가 ㅡㅡ;;;;

몇일 놀다가 다시 쓴당(2022.08.16)

import random

rock = '''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

paper = '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
'''

scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''

#Write your code below this line 👇
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"))
if user_choice >=3 or user_choice < 0:
  print("you typed an invalid number, you lose!")
else:
  print("You choice :\n")
  print(game_images[user_choice])

computer_choice = random.randint(0,2)
print("computer chose :\n")
print(game_images[computer_choice])



if 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 computer_choice == user_choice:
  print("It's a draw")

확실히 간결 해졌다... 단순한 산수 문제 일수 있는데 리스트 를 사용 하고 elif 를 통해 반복비교 했다.

난 역시 코딩 경험이 훨씬 더 많이 필요함을 느낀다.

 

728x90
반응형
댓글