2 분 소요

input

age = input("How old are you?")

print("user answer", age)

image

  • input function는 사용자에게 질문이 표시되는 function이다.
  • input function는 오직 1개의 argumnet만 받을 수 있다.
  • 만약 사용자가 질문에 답하면 그 value가 return하면서 input function은 사용자가 답한 value가 된다.

type( )

age = input("How old are you?")

print(type(age))

  • type function는 variable의 type을 알려주는 function이다.

int

age = int(input("How old are you?"))

if age < 18:
  print("you can't drink")

elif age > 18 and age <35: 
	print("you drink beer!"

else:
  print("Go ahead!")

image

  • int function는 string을 정수로 바꿔주는 function이다.

and

age = int(input("How old are you?"))

if age < 18:
  print("you can't drink")

elif age > 18 and age <35: 
	print("you drink beer!")

else:
  print("Go ahead!")

image

  • and는 elif나 if에서 동시에 두가지 조건을 확인해 줄수있다.
  • 동시에 두가지 조건을 보기 때문에 한곳만 false해도 다 false이게 된다.
  • 그리고 양쪽 다 true이여야만 true로 인식한다.

or

age = int(input("How old are you?"))

if age < 18:
  print("you can't drink")

elif age > 18 and age < 35:
  print("you drink beer!")

elif age == 60 or age == 70:
  print("Birthday party")

else:
  print("Go ahead!")

image

  • or는 앞부분이나 뒷부분이 true인지 확인하는 것이다.
  • or는 1개만 true이여도 true으로 인식한다.
True and True == True
False and True == False
True and False ==False
False and False == False

True or True == True
True or False == True
False or True == True
False or False == False

casino

user_choice = int(input("Chose number."))
pc_choice = 50

if user_choice == pc_choice:
  print("You won!")
elif user_choice > pc_choice:
  print("You Lose!")
elif user_choice < pc_choice:
  print("Higer!")

image

  • pc_choice값보다 user_choice값이 작으면 Higer, 크면 You Lose!, 같으면 You won!
  • built-in function 이란 항상 사용할 수 있는 fucntion.(따로작성 안해도됨)

  • 파이썬 표준 라이브러리 문서 : \https://docs.python.org/ko/3/library/index.html\

randint(a,b)

  • a, b사이의 정수를 랜덤으로 나타내줌
  • from random import randint 을 해줘야함
  • from이 모듈에서 이 함수를 import 해줘!
from random import randint

user_choice = int(input("Chose number."))
pc_choice = randint(1, 50)

if user_choice == pc_choice:
  print("One More Time!", )
elif user_choice < pc_choice:
  print("You Lose! Computer chose", pc_choice)
elif user_choice > pc_choice:
  print("You Won! Computer chose", pc_choice)

image

while

  • if와 비슷하지만 중지할때까지 계속 진행 됨
distance = 0

while distance < 20:
  print("I'm running: distance =", distance, "km")
  distance = distance + 1
  1. distance는 0부터 시작되고, distance의 값이 20보다 작으므로 True → 코드진행
  2. print( )를 출력한 다음, distance의 현재값에 1를 더해줌 → 0+1 = 1
  3. while문이므로 계속 진행 → distance의 값(1)이 20보다 작으므로 True → 코드진행
    1. print( )를 출력한 다음, distance의 현재값에 1를 더해줌 → 1+1 = 2
  4. 계속하다가 distance의 값이 20이되면 20보다 작다라는 조건문에서 False 반환 → 코드 멈춤

주석 작성

  • 주석 작성: # 해쉬태그 작성하고 쓰면 됨
  • 주석 문단으로 작성: “”” : 3개의 쌍따옴표를 맨 앞과 맨 뒤에 적어주면 됨

최종 Python Casino program = pc_choice의 값 맞추기

from random import randint

print("Welcome Python Casino")

pc_choice = randint(1, 100)

playing = True

while playing:
  user_choice = int(input("Chose number. 1-100:"))
  if user_choice == pc_choice:
    print("You Won", "pc_choice =", pc_choice)
    playing = False
  elif user_choice < pc_choice:
    print("Higher!", "pc_choice =", pc_choice)
  elif user_choice > pc_choice:
    print("Lower!", "pc_choice =", pc_choice)

image

댓글남기기