Hỏi ý kiến nhận xét chương trình python

em mới học python và tự thử thực hành code game đấm lá kéo. Mong anh chị bớt chút thời gian nhận xét chương trình em làm ạ, em cảm ơn ạ

while True:
    Options = ['có','không']
    print("- - "*10)
    print("Hãy chọn đấm, lá hoặc kéo")
    player = input("Player:")
    import random
    # Danh sách các biến
    options = ['đấm', 'lá', 'kéo']
    # Trộn danh sách và lấy phần tử đầu tiên
    random.shuffle(options)
    computer = options[0]
    if player in options:
        if player == "đấm":
            print("Computer:"+ computer)
            if computer=="lá":
                print("You Lose")
            elif computer=="kéo":
                print("You Win")
            elif computer == player:
                print("Draw")
        if player == "lá":
            print("Computer:"+ computer)
            if computer=="kéo":
                print("You Lose")
            elif computer=="đấm":
                print("You Win")
            elif computer == player:
                print("Draw")
        if player == "kéo":
            print("Computer:"+ computer)
            if computer=="đấm":
                print("You Lose")
            elif computer=="lá":
                print("You Win")
            elif computer == player:
                print("Draw")
    if player not in options:
        print("Hãy thử lại")          
       
    Player = ""
    print("- - "*10)
    print("Bạn có muốn chơi tiếp không")
    while Player not in Options:    
        Player = input()
       
        if Player not in Options:
            print("Hãy thử lại")
            print("- - "*10)
            print("Bạn có muốn chơi tiếp không")
       
    if Player in Options:
        if Player=="có":
            True
        if Player=="không":
            print("Kết thúc trò chơi, hẹn gặp lại")
            break

bạn cần nhận xét về mặt nào? ý tưởng, nội dung, hay cách viết code?

image




mình hơi lười nên đã nhờ chatgpt nhận xét, bạn thấy nhận xét này thế nào ?

2 Likes

mình xin ý kiến về nội dung code ạ, ý mình hỏi là code này đã đc tối ưu so với ý tưởng chưa ạ

câu trả lời từ chatgpt có làm bạn hài lòng không?

đây có phải là câu hỏi yes/no không? bạn mong đợi câu trả lời yes/no?

theo bạn, tối ưu là gì?

chatgpt nhận xét cũng đúng, mình chưa chạy chương trình chatgpt mà bạn chụp nhưng cũng chạy thử một chương trình khác của copilot thì thấy không phù hợp với ý tưởng của mình. Nói chung là mình cũng hài lòng với câu trả lời của chat gpt rồi. theo mình thì tối ưu ở đây là code đã sát ý tưởng chưa, đây có phải là đoạn code ngắn gọn mà vẫn hiệu quả không,… dạng vậy á

mình nhờ chatgpt nhận xét dùm bạn chứ có chạy gì đâu

vậy có ai biết ý tưởng của bạn là gì đâu mà nhận xét

hay ý tưởng của bạn là đây, “game đấm lá kéo” là ý tưởng hay là đề bài/vấn đề?

theo bạn, tối ưu là gì? bạn thì lại trả lời là ngắn gọn và hiệu quả, lại lòi ra thêm 2 khái niệm không rõ ràng khác
bao nhiêu dòng code là ngắn? chương trình chạy như thế nào hiệu quả (vậy như thế nào là chưa hiệu quả)?

Since you’re a newbie, I have a few tips for you:

  1. Development means creating something from scratch, starting with a rough idea until the goal is achieved.
  2. Then, check whether the goal fulfills all the desired functions.
  3. If everything is to your satisfaction, you need to go one step further and look for other solutions that could lead to the same goal.
  4. If there are any, evaluate them, select the most efficient one, and optimize it.

Your approach is only the first step. It seems to me that you lack confidence because you constantly question whether your solution is good or bad, and this leads you nowhere, keeping you stuck in a dead end. The reason for this is that the proposed solutions are NOT your own, and you have to study them first to understand them. This can lead to confusion or misunderstanding. So you learn nothing, and your confidence dwindles with each question.

Just one example, alongside the ChatGPT code: Your code starts with an infinite loop that breaks if the player says NO. Another solution could be:

import random
def playing(idx):
    iPlayer = options.index(Player)
    if (iPlayer < idx) or (iPlayer == 2 and idx == 0):
        print("You Lose")
    elif iPlayer == idx:
        print("Draw")
    else:
        print("You Win")

Options = ['có','không']
options = ['đấm', 'lá', 'kéo']
Player = 'có'
while Player == 'có':
    print("- - "*10)
    print("select đấm, lá, kéo")
    Player = input()
    if Player in options:
        playing(random.randrange(3))
    else:
        print("Unknown:"+Player)
    print("- - "*10)
    print("Continue with 'có', 'không' ")
    while Player not in Options:    
        Player = input()
print("Kết thúc trò chơi, hẹn gặp lại")

Repeated assignment of Options and options is avoided. The check to see if the player still wants to play is now done in the outer while loop.

or you can code directly without def

import random
Options = ['có','không']
options = ['đấm', 'lá', 'kéo']
Player = 'có'
while Player == 'có':
    print("- - "*10)
    print("select đấm, lá, kéo")
    Player = input()
    if Player in options:
        idx = random.randrange(3)
        print("Computer:"+ options[idx])
        iPlayer = options.index(Player)
        if (iPlayer < idx) or (iPlayer == 2 and idx == 0):
            print("You Lose")
        elif iPlayer == idx:
            print("Draw")
        else:
            print("You Win")
    else:
        print("Unknown:"+Player)
    print("- - "*10)
    print("Continue with 'có', 'không' ")
    while Player not in Options:    
        Player = input()

print("Kết thúc trò chơi, hẹn gặp lại")
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?