19  Chatgpt Mentor

20 ChatGPT: 개인 Python 코딩 멘토

이 저장소에는 몇 가지 추가 파일과 함께 Real Python ChatGPT: 개인 Python 코딩 멘토 튜토리얼에 사용된 코드가 보관되어 있습니다.

20.1 스크립트 실행

모든 예제 스크립트는 인터프리터로 파일을 실행하여 Python 버전 >=3.6에서 실행되어야 합니다.

$ python filename.py

FizzBuzz의 다양한 구현을 사용하려면 ‘-i’ 플래그를 사용하여 대화형으로 REPL에 파일을 로드할 수 있습니다.

20.2 프롬프트

저장소에는 튜토리얼에 사용된 모든 프롬프트를 나열하는 prompts.md라는 추가 파일도 포함되어 있습니다. 자신의 ChatGPT 대화에서 이를 시도하고 싶다면 해당 파일에서 프롬프트를 복사할 수 있습니다.

20.3 작가

20.4 라이선스

MIT 라이센스에 따라 배포됩니다. 자세한 내용은 LICENSE를 참조하세요.

20.5 파일: fizzbuzz.py

def fizzbuzz(number):
    if number % 3 == 0:
        return "fizz"
    elif number % 5 == 0:
        return "buzz"
    elif number % 15 == 0:
        return "fizz buzz"
    else:
        return number

20.6 파일: fizzbuzz_chatgpt_option_1.py

def fizzbuzz(n):
    for i in range(1, n + 1):
        if i % 15 == 0:
            print("fizz buzz")
        elif i % 3 == 0:
            print("fizz")
        elif i % 5 == 0:
            print("buzz")
        else:
            print(i)

20.7 파일: fizzbuzz_chatgpt_option_2.py

def fizzbuzz(n):
    result = [
        (
            "fizz buzz"
            if i % 15 == 0
            else "fizz"
            if i % 3 == 0
            else "buzz"
            if i % 5 == 0
            else i
        )
        for i in range(1, n + 1)
    ]
    return result

20.8 파일: fizzbuzz_chatgpt_option_3.py

def fizzbuzz(n):
    return (
        (
            "fizz buzz"
            if i % 15 == 0
            else "fizz"
            if i % 3 == 0
            else "buzz"
            if i % 5 == 0
            else i
        )
        for i in range(1, n + 1)
    )

20.9 파일: fizzbuzz_chatgpt_option_4.py

def fizzbuzz(n):
    mapping = {3: "fizz", 5: "buzz", 15: "fizz buzz"}
    for i in range(1, n + 1):
        output = ""
        for key in mapping:
            if i % key == 0:
                output += mapping[key]
        if output == "":
            output = i
        print(output)

20.10 파일: guess_final.py

import random


def guess_the_number(low: int = 1, high: int = 100) -> int:
    """
    Plays a guessing game where the user must guess a random number between
    `low` and `high`. Returns the randomly generated number.

    Args:
        low (int): The lowest possible number in the range (default 1).
        high (int): The highest possible number in the range (default 100).

    Returns:
        int: The randomly generated number.

    """
    random_number = random.randint(low, high)
    user_guess = None

    while user_guess != random_number:
        user_guess = int(input(f"Guess a number between {low} and {high}: "))
        if user_guess < random_number:
            print("Your guess is too low. Try again!")
        elif user_guess > random_number:
            print("Your guess is too high. Try again!")
        else:
            print("Congratulations, you guessed the number correctly!")

    return random_number

20.11 파일: guess_initial.py

import random

num = random.randint(1, 100)
i = None

while i != num:
    i = int(input("Guess the number: "))
    if i < num:
        print("Too low")
    elif i > num:
        print("Too high")
    else:
        print("Correct!")

20.12 파일: recursion_error.py

# Here's another interesting example for debugging your code:


class Stack(list):
    def push(self, item):
        self.append(item)

    def pop(self):
        return super().pop()

    def __repr__(self) -> str:
        return f"{type(self).__name__}({self})"


print(Stack([1, 2, 3]))

# Try the following prompt and see how ChatGPT guides you to a solution:

"""
I have a Python class:

class Stack(list):
    def push(self, item):
        self.append(item)
    def pop(self):
        return super().pop()
    def __repr__(self) -> str:
        return f"{type(self).__name__}({self})"

When I call repr() with an instance of this class,
I get <repr-error 'maximum recursion depth exceeded'>

Can you help me fix that?
"""