Computer Programming/AI

TIL_Python Assignment 1(Up-and-Down Game)

JYCoder 2023. 8. 21. 19:33

저번 주에 python 문법 강의를 마치고 오늘 처음으로 개인과제를 했다.

제목은 up-and-down game. python을 이용한 게임을 만드는 것이다.

 

컴퓨터가 생각한 숫자를 맞추는 게임이다. 컴퓨터가 생각한 숫자와 비교하여 Up 또는 Down 힌트를 받으며 숫자를 맞추는 룰이다. 이 게임을 위해 내가 사용한 python 기능은 다음과 같다.

while, try-except, if-elif, random, input() 등.

 

import random

random_num = random.randint(1, 100)
count = 0
highest_count = 0
while True:
    try:
        input_num = int(input("Please enter a number(1-100): "))
        count += 1
        if input_num > random_num:
            print('Down')

        elif input_num < random_num:
            print('Up')

        else:
            if count >= highest_count:
                highest_count = count
            print('Great!')
            print('Current count: ', count)
            print('Highest count: ', highest_count)
            answer = input("Do you want to try again?(yes/no) ")
            if answer == 'yes':
                random_num = random.randint(1, 100)
                count = 0
            else:
                print('Bye!')
                break

    except ValueError:
        print("Oops!Type valid number. Try again...")

 

아주 심플한 게임이었지만, 머릿 속에 있는 아이디어를 직접 실현시키는 작업이 생각보다 많이 어려웠다.

강의를 보고 복습하는 것도 좋지만, 이렇게 직접 프로그램을 만들어 보는 경험이 아주 중요할 것 같다.

더욱 열심히 프로그래밍을 만들어야겠다.

LIST