1. 파이썬 랜덤모듈을 이용할겁니다.
파이썬 관련 모듈과 튜토리얼이 있어 많이 도움되는 사이트 입니다.
https://www.w3schools.com/python/module_random.asp
Python Random Module
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
https://docs.python.org/ko/3/library/random.html#module-random
random — Generate pseudo-random numbers
Source code: Lib/random.py This module implements pseudo-random number generators for various distributions. For integers, there is uniform selection from a range. For sequences, there is uniform s...
docs.python.org
랜덤모듈을 살펴보면, 자세히 설명되어 있어요. 여기서는 random.randint를 사용합니다.
2. 컴퓨터와 하는 번호 맞추기 게임을 해볼까요?
가. 컴퓨터가 무작위로 번호 하나를 생성하면
나. 제가 숫자를 써서 맞추는 거에요. 간단하죠?
import random
def guess(x):
random_number = random.randint(1,x)
guess=0
while(guess != random_number ):
guess = int(input(f'번호를 맞춰보세요:1~{x}: '))
if guess < random_number:
print('땡!! 틀렸습니다. 숫자가 너무 작아요')
elif guess> random_number:
print('땡!! 틀렸습니다. 숫자가 너무 커요.')
else:
print('정답입니다!')
guess(10)
import로 random 모듈을 불러오고 def guess(x)함수를 만들었어요.
while문으로 정답일때까지 반복하게 만들었어요.
3. 파이썬 프로젝트는 이곳을 참고했습니다.
'파이썬 > 파이썬 게임 만들기' 카테고리의 다른 글
파이썬 3단계 : 번호 맞추기 게임 (컴퓨터) (0) | 2024.03.30 |
---|---|
파이썬 1단계 : 메드립스(문장 만들기) 게임 (0) | 2024.03.30 |