개발/python

#3.7 Recap

규지니어스 2022. 9. 13. 22:05

from random import randint, uniform

print("Welcome to Python Casino")
pc_choice = randint(1, 100)

playing = True

while playing:
  user_choice = int(input("Choose number(1-100) > "))
  if user_choice == pc_choice:
    print("You Won!")

    playing = False 
  elif user_choice > pc_choice:
    print("Lower!")
  elif user_choice < pc_choice:
    print("Higher!")

----------------------------------------------------------------

from random import randint, uniform
: 모듈(파일)에서 함수 가져오기
random 이라는 모듈은 Python standard Library 안에 있다.
함수는 우리가 사용할 수 있는 아주 많은 기능들이 있다.

* int input print 과 같은 함수는 built in 함수라 항상 사용할 수 있다

* 또 포함되지 않는 함수들이 많은데 파이썬에는 모듈이 딸려있고 많은 함수들이 그 모듈안에 포함되어 있기 때문이다



randint : 랜덤하게 정수를 만들어냄


print("Welcome to Python Casino")
pc_choice = randint(1, 100)

playing = True
---------------------
2. 반복문 while
while이 조건이 true일 경우 조건을 만족할 때 까지 코드반복         그래서 아래 while 문에 user가 이긴다면 멈춰야 하기에 False를 넣음.                     

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


import, while

* 파이썬 모듈 random 안에는 많은 함수를 내포하고 있다.
from random import randint
=> random이라는 모듈안에 randint 라는 함수를 가져온다.

randint 함수를 import하고 사용하기 위해 argument 2개를 넣어 사용
pc_choice = randint(1, 100)

while playing:
playing이 true 라면 반복해서 코드실행
    playing = False => while문을 멈춘 곳


잘한 점: 
모듈과 함수/ 디테일하게 구분할 줄 알게되었다

아쉬운 점: 같은 내용을 설명하고자 하면 여러번을 반복해서 외우기만 하려고 하는 것?

'개발 > python' 카테고리의 다른 글

#4.1 Lists  (0) 2022.09.15
#4.0 Methods  (0) 2022.09.14
#3.2 Recap 기본파라미터, If else elif 복습_9월5일  (0) 2022.09.05
#3.1 Else & elif_9월4일  (0) 2022.09.04
#2.9 Default Parameters 기본 파라미터_9월3일  (0) 2022.09.03