Hướng dẫn what is rock paper scissors game in python? - trò chơi oẳn tù tì trong python là gì?

Lập trình trò chơi là một cách tuyệt vời để học cách lập trình. Bạn sử dụng nhiều công cụ mà bạn sẽ thấy trong thế giới thực, cộng với bạn có thể chơi một trò chơi để kiểm tra kết quả của mình! Một trò chơi lý tưởng để bắt đầu hành trình lập trình trò chơi Python của bạn là kéo giấy đá.

Show

Trong hướng dẫn này, bạn sẽ học cách::

  • Mã trò chơi kéo giấy đá của riêng bạnrock paper scissors game
  • Đưa vào đầu vào của người dùng với
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
    
    8
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
    
    8
  • Chơi một số trò chơi liên tiếp bằng cách sử dụng vòng lặp
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
    
    9
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
    
    9 loop
  • Làm sạch mã của bạn bằng
    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")
    
    0 và các chức năng
    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")
    
    0
    and functions
  • Xác định các quy tắc phức tạp hơn với từ điểndictionary

Kéo giấy đá là gì?

Bạn có thể đã chơi kéo giấy đá trước đây. Có lẽ bạn đã sử dụng nó để quyết định ai trả tiền cho bữa tối hoặc ai là người đầu tiên lựa chọn người chơi cho một đội.

Nếu bạn không quen thuộc, Rock Paper kéo là một trò chơi cầm tay cho hai hoặc nhiều người chơi. Những người tham gia nói rằng đá, giấy, kéo kéo và sau đó đồng thời hình thành tay của họ thành hình dạng của một tảng đá (một nắm tay), một mảnh giấy (lòng bàn tay hướng xuống dưới), hoặc một cặp kéo (hai ngón tay mở rộng). Các quy tắc rất đơn giản:

  • Đá đập vỡ kéo. smashes scissors.
  • Giấy bao gồm đá. covers rock.
  • Kéo cắt giấy. cut paper.

Bây giờ bạn có các quy tắc xuống, bạn có thể bắt đầu suy nghĩ về cách họ có thể dịch sang mã Python.

Chơi một trò chơi kéo bằng giấy đá trong Python

Sử dụng mô tả và quy tắc ở trên, bạn có thể tạo ra một trò chơi kéo giấy đá. Trước khi bạn lao vào, bạn sẽ cần nhập mô -đun mà bạn sẽ sử dụng để mô phỏng các lựa chọn máy tính:

Đáng kinh ngạc! Bây giờ, bạn có thể sử dụng các công cụ khác nhau bên trong

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
1 để ngẫu nhiên các hành động của máy tính trong trò chơi. Giờ thì sao? Vì người dùng của bạn cũng sẽ cần có khả năng chọn hành động của họ, điều logic đầu tiên bạn cần là một cách để đưa vào đầu vào của người dùng.

Lấy đầu vào của người dùng

Lấy đầu vào từ người dùng là khá đơn giản trong Python. Mục tiêu ở đây là hỏi người dùng những gì họ muốn chọn làm hành động và sau đó gán lựa chọn đó cho một biến:

user_action = input("Enter a choice (rock, paper, scissors): ")

Điều này sẽ nhắc người dùng nhập lựa chọn và lưu nó vào một biến để sử dụng sau. Bây giờ người dùng đã chọn một hành động, máy tính cần quyết định phải làm gì.

Làm cho máy tính chọn

Một trò chơi cạnh tranh của kéo giấy đá liên quan đến chiến lược. Tuy nhiên, thay vì cố gắng phát triển một mô hình cho điều đó, bạn có thể tiết kiệm cho mình một thời gian bằng cách để máy tính chọn một hành động ngẫu nhiên. Các lựa chọn ngẫu nhiên là một cách tuyệt vời để máy tính chọn giá trị giả.

Bạn có thể sử dụng

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
2 để chọn máy tính ngẫu nhiên giữa các hành động:
if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
2
to have the computer randomly select between the actions:

possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)

Điều này cho phép một yếu tố ngẫu nhiên được chọn từ danh sách. Bạn cũng có thể in các lựa chọn mà người dùng và máy tính thực hiện:

print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

In các hành động của người dùng và máy tính có thể hữu ích cho người dùng và nó cũng có thể giúp bạn gỡ lỗi sau này trong trường hợp có điều gì đó hoàn toàn đúng với kết quả.

Xác định một người chiến thắng

Bây giờ cả hai người chơi đã đưa ra lựa chọn của mình, bạn chỉ cần một cách để quyết định ai thắng. Sử dụng khối

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
3 ____ ____34 ____ ____35, bạn có thể so sánh các lựa chọn của người chơi và xác định người chiến thắng:

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")

Bằng cách so sánh điều kiện cà vạt trước, bạn sẽ thoát khỏi khá nhiều trường hợp. Nếu bạn đã làm điều đó, thì bạn cần phải kiểm tra từng hành động có thể cho

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
6 và so sánh nó với từng hành động có thể xảy ra với
if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
7. Bằng cách kiểm tra điều kiện cà vạt trước, bạn có thể biết máy tính đã chọn chỉ với hai kiểm tra có điều kiện là
if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
7.

Và đó là nó! Tất cả kết hợp, mã của bạn bây giờ sẽ trông như thế này:

import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")

Bây giờ bạn đã viết mã để lấy đầu vào của người dùng, chọn một hành động ngẫu nhiên cho máy tính và quyết định người chiến thắng! Nhưng điều này chỉ cho phép bạn chơi một trò chơi trước khi chương trình kết thúc.

Chơi một số trò chơi liên tiếp

Mặc dù một trò chơi kéo bằng giấy đá là siêu thú vị, nhưng sẽ tốt hơn nếu bạn có thể chơi một số trò chơi liên tiếp? Vòng lặp là một cách tuyệt vời để tạo ra các sự kiện định kỳ. Cụ thể, bạn có thể sử dụng vòng lặp

print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
9 để chơi vô thời hạn:Loops are a great way to create recurring events. In particular, you can use a
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
9 loop to play indefinitely:

import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break

Lưu ý các dòng được tô sáng ở trên. Điều quan trọng là phải kiểm tra xem người dùng có muốn chơi lại không và

import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
0 nếu họ không. Nếu không có kiểm tra đó, người dùng sẽ bị buộc phải chơi cho đến khi họ chấm dứt bảng điều khiển bằng CTRL+C hoặc phương thức tương tự.Ctrl+C or a similar method.

Kiểm tra để chơi lại là một kiểm tra đối với chuỗi

import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
1. Nhưng việc kiểm tra một cái gì đó cụ thể như thế này có thể khiến người dùng ngừng chơi khó khăn hơn. Điều gì sẽ xảy ra nếu người dùng loại
import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
2 hoặc
import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
3? So sánh chuỗi thường khó khăn vì bạn không bao giờ biết người dùng có thể nhập gì. Họ có thể làm tất cả các chữ thường, tất cả chữ hoa, hoặc thậm chí là một hỗn hợp của cả hai.

Dưới đây là kết quả của một vài so sánh chuỗi khác nhau:

>>>

>>> play_again = "yes"
>>> play_again == "n"
False
>>> play_again != "y"
True

Hmm. Đó không phải là những gì bạn muốn. Người dùng có thể không quá hạnh phúc nếu họ nhập

import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
2 mong đợi sẽ chơi lại nhưng bị đá từ trò chơi.

Mô tả một hành động với import random user_action = input("Enter a choice (rock, paper, scissors): ") possible_actions = ["rock", "paper", "scissors"] computer_action = random.choice(possible_actions) print(f"\nYou chose {user_action}, computer chose {computer_action}.\n") if user_action == computer_action: print(f"Both players selected {user_action}. It's a tie!") elif user_action == "rock": if computer_action == "scissors": print("Rock smashes scissors! You win!") else: print("Paper covers rock! You lose.") elif user_action == "paper": if computer_action == "rock": print("Paper covers rock! You win!") else: print("Scissors cuts paper! You lose.") elif user_action == "scissors": if computer_action == "paper": print("Scissors cuts paper! You win!") else: print("Rock smashes scissors! You lose.") 5

Bởi vì so sánh chuỗi có thể gây ra các vấn đề như bạn đã thấy ở trên, nên nó là một ý tưởng tốt để tránh chúng bất cứ khi nào có thể. Tuy nhiên, một trong những điều đầu tiên mà chương trình của bạn yêu cầu là người dùng nhập chuỗi! Điều gì sẽ xảy ra nếu người dùng nhập

import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
6 hoặc
import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
7 do nhầm lẫn? Các vấn đề về vốn hóa, vì vậy họ đã thắng được bằng nhau:

>>>

>>> print("rock" == "Rock")
False

Vì các vấn đề về vốn hóa,

import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
8 và
import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
9 aren bằng nhau. Một giải pháp khả thi là sử dụng các số thay thế. Chỉ định từng hành động mà một số có thể giúp bạn tiết kiệm một số rắc rối:

ROCK_ACTION = 0
PAPER_ACTION = 1
SCISSORS_ACTION = 2

Điều này cho phép bạn tham khảo các hành động khác nhau theo số được chỉ định của họ. Các số nguyên don don phải chịu các vấn đề so sánh tương tự như chuỗi, vì vậy điều này có thể hoạt động. Bây giờ bạn có thể yêu cầu người dùng nhập một số và so sánh trực tiếp với các giá trị đó:

user_input = input("Enter a choice (rock[0], paper[1], scissors[2]): ")
user_action = int(user_input)
if user_action == ROCK_ACTION:
    # Handle ROCK_ACTION

print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
8 Trả về một chuỗi, bạn cần chuyển đổi giá trị trả về thành số nguyên bằng
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
1. Sau đó, bạn có thể so sánh đầu vào với từng hành động trên. Điều này hoạt động tốt, nhưng nó có thể dựa vào các biến đặt tên của bạn một cách chính xác để theo dõi chúng. Một cách tốt hơn là sử dụng
import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
5 và xác định lớp hành động của riêng bạn!
import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
5
and define your own action class!

Sử dụng

import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
5 cho phép bạn tạo các thuộc tính và gán cho chúng các giá trị tương tự như các giá trị được hiển thị ở trên. Điều này giúp làm sạch mã của bạn bằng cách nhóm các hành động thành các không gian tên của riêng họ và làm cho mã biểu cảm hơn:

possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
0

Điều này tạo ra một tùy chỉnh

import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4 mà bạn có thể sử dụng để tham khảo các loại hành động khác nhau mà bạn hỗ trợ. Nó hoạt động bằng cách gán từng thuộc tính trong đó cho một giá trị bạn chỉ định.

So sánh vẫn đơn giản, và bây giờ chúng có một tên lớp hữu ích được liên kết với chúng:

>>>

possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
1

Vì các vấn đề về vốn hóa,

import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
8 và
import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
9 aren bằng nhau. Một giải pháp khả thi là sử dụng các số thay thế. Chỉ định từng hành động mà một số có thể giúp bạn tiết kiệm một số rắc rối:

Điều này cho phép bạn tham khảo các hành động khác nhau theo số được chỉ định của họ. Các số nguyên don don phải chịu các vấn đề so sánh tương tự như chuỗi, vì vậy điều này có thể hoạt động. Bây giờ bạn có thể yêu cầu người dùng nhập một số và so sánh trực tiếp với các giá trị đó:

>>>

possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
2

Vì các vấn đề về vốn hóa,

import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
8 và
import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
9 aren bằng nhau. Một giải pháp khả thi là sử dụng các số thay thế. Chỉ định từng hành động mà một số có thể giúp bạn tiết kiệm một số rắc rối:

Điều này cho phép bạn tham khảo các hành động khác nhau theo số được chỉ định của họ. Các số nguyên don don phải chịu các vấn đề so sánh tương tự như chuỗi, vì vậy điều này có thể hoạt động. Bây giờ bạn có thể yêu cầu người dùng nhập một số và so sánh trực tiếp với các giá trị đó:

print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
8 Trả về một chuỗi, bạn cần chuyển đổi giá trị trả về thành số nguyên bằng
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
1. Sau đó, bạn có thể so sánh đầu vào với từng hành động trên. Điều này hoạt động tốt, nhưng nó có thể dựa vào các biến đặt tên của bạn một cách chính xác để theo dõi chúng. Một cách tốt hơn là sử dụng
import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
5 và xác định lớp hành động của riêng bạn!flowchart of the desired behavior and implement code around it. You could achieve a similar result using a bulleted list, but it’d be harder to capture things like loops and conditional logic.

Sử dụng

import random

user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
5 cho phép bạn tạo các thuộc tính và gán cho chúng các giá trị tương tự như các giá trị được hiển thị ở trên. Điều này giúp làm sạch mã của bạn bằng cách nhóm các hành động thành các không gian tên của riêng họ và làm cho mã biểu cảm hơn:

Điều này tạo ra một tùy chỉnh

import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4 mà bạn có thể sử dụng để tham khảo các loại hành động khác nhau mà bạn hỗ trợ. Nó hoạt động bằng cách gán từng thuộc tính trong đó cho một giá trị bạn chỉ định.

Hướng dẫn what is rock paper scissors game in python? - trò chơi oẳn tù tì trong python là gì?

So sánh vẫn đơn giản, và bây giờ chúng có một tên lớp hữu ích được liên kết với chúng:

Bởi vì các giá trị thành viên là như nhau, so sánh là bằng nhau. Các tên lớp cũng làm cho nó rõ ràng hơn rằng bạn muốn so sánh hai hành động.

Bạn thậm chí có thể tạo một

import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4 từ
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
6:

Hướng dẫn what is rock paper scissors game in python? - trò chơi oẳn tù tì trong python là gì?

import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4 nhìn vào giá trị được truyền vào và trả về
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4 thích hợp. Điều này rất hữu ích vì bây giờ bạn có thể nhận đầu vào của người dùng dưới dạng
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
6 và tạo
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4 từ nó. Không còn lo lắng về chính tả!

Dòng chảy (biểu đồ) của chương trình của bạn

Mặc dù kéo giấy đá có vẻ không phức tạp, nhưng điều quan trọng là phải xem xét cẩn thận các bước liên quan đến việc chơi nó để bạn có thể chắc chắn rằng chương trình của bạn bao gồm tất cả các kịch bản có thể. Đối với bất kỳ dự án nào, ngay cả những dự án nhỏ, nó cũng hữu ích để tạo ra một sơ đồ hành vi mong muốn và thực hiện mã xung quanh nó. Bạn có thể đạt được một kết quả tương tự bằng cách sử dụng danh sách đạn, nhưng nó khó có thể nắm bắt được những thứ như vòng lặp và logic có điều kiện.

Sơ đồ don don phải quá phức tạp hoặc thậm chí sử dụng mã thực. Chỉ cần mô tả hành vi mong muốn trước thời hạn có thể giúp bạn khắc phục sự cố trước khi chúng xảy ra!

possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
3

Hy vọng rằng tất cả điều này có vẻ quen thuộc cho đến nay! Bây giờ, ở đây, mã cho

>>> play_again = "yes"
>>> play_again == "n"
False
>>> play_again != "y"
True
3, không có bất kỳ đối số nào và trả về
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4:

possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
4

Lưu ý cách bạn lấy đầu vào của người dùng dưới dạng

import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
6 và lấy lại
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4. Tuy nhiên, thông báo dài cho người dùng là hơi cồng kềnh. Điều gì sẽ xảy ra nếu bạn muốn thêm nhiều hành động? Bạn phải thêm nhiều văn bản hơn vào dấu nhắc.

Thay vào đó, bạn có thể sử dụng danh sách hiểu biết để tạo một phần của đầu vào:

possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
5

Bây giờ bạn không còn cần phải lo lắng về việc thêm hoặc loại bỏ các hành động trong tương lai! Kiểm tra điều này, bạn có thể thấy cách mã nhắc người dùng và trả về một hành động được liên kết với giá trị đầu vào của người dùng:

>>>

possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
6

Bây giờ bạn cần một chức năng để có được hành động máy tính. Giống như

>>> play_again = "yes"
>>> play_again == "n"
False
>>> play_again != "y"
True
3, chức năng này không có đối số và trả lại
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4. Bởi vì các giá trị cho
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4 phạm vi từ
>>> print("rock" == "Rock")
False
0 đến
>>> print("rock" == "Rock")
False
1, bạn sẽ muốn tạo một số ngẫu nhiên trong phạm vi đó.
>>> print("rock" == "Rock")
False
2 có thể giúp với điều đó.
>>> print("rock" == "Rock")
False
2
can help with that.

>>> print("rock" == "Rock")
False
2 Trả về một giá trị ngẫu nhiên giữa một mức tối thiểu và tối đa được chỉ định (bao gồm). Bạn có thể sử dụng
>>> print("rock" == "Rock")
False
4 để giúp tìm ra giới hạn trên nên có trong mã của bạn:

possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
7

Bởi vì các giá trị

import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4 bắt đầu đếm từ
>>> print("rock" == "Rock")
False
0 và Len () bắt đầu đếm từ
>>> print("rock" == "Rock")
False
7, điều quan trọng là phải làm
>>> print("rock" == "Rock")
False
8.

Khi bạn kiểm tra điều này, đã giành được một lời nhắc. Nó chỉ đơn giản là trả về hành động được liên kết với số ngẫu nhiên:

>>>

possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
8

Bây giờ bạn cần một chức năng để có được hành động máy tính. Giống như

>>> play_again = "yes"
>>> play_again == "n"
False
>>> play_again != "y"
True
3, chức năng này không có đối số và trả lại
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4. Bởi vì các giá trị cho
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4 phạm vi từ
>>> print("rock" == "Rock")
False
0 đến
>>> print("rock" == "Rock")
False
1, bạn sẽ muốn tạo một số ngẫu nhiên trong phạm vi đó.
>>> print("rock" == "Rock")
False
2 có thể giúp với điều đó.

possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
9

>>> print("rock" == "Rock")
False
2 Trả về một giá trị ngẫu nhiên giữa một mức tối thiểu và tối đa được chỉ định (bao gồm). Bạn có thể sử dụng
>>> print("rock" == "Rock")
False
4 để giúp tìm ra giới hạn trên nên có trong mã của bạn:

Bởi vì các giá trị

import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4 bắt đầu đếm từ
>>> print("rock" == "Rock")
False
0 và Len () bắt đầu đếm từ
>>> print("rock" == "Rock")
False
7, điều quan trọng là phải làm
>>> print("rock" == "Rock")
False
8.

>>>

print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
0

Bây giờ bạn cần một chức năng để có được hành động máy tính. Giống như

>>> play_again = "yes"
>>> play_again == "n"
False
>>> play_again != "y"
True
3, chức năng này không có đối số và trả lại
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4. Bởi vì các giá trị cho
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4 phạm vi từ
>>> print("rock" == "Rock")
False
0 đến
>>> print("rock" == "Rock")
False
1, bạn sẽ muốn tạo một số ngẫu nhiên trong phạm vi đó.
>>> print("rock" == "Rock")
False
2 có thể giúp với điều đó.

>>>

print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
1

Bây giờ bạn cần một chức năng để có được hành động máy tính. Giống như

>>> play_again = "yes"
>>> play_again == "n"
False
>>> play_again != "y"
True
3, chức năng này không có đối số và trả lại
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4. Bởi vì các giá trị cho
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4 phạm vi từ
>>> print("rock" == "Rock")
False
0 đến
>>> print("rock" == "Rock")
False
1, bạn sẽ muốn tạo một số ngẫu nhiên trong phạm vi đó.
>>> print("rock" == "Rock")
False
2 có thể giúp với điều đó.

>>> print("rock" == "Rock")
False
2 Trả về một giá trị ngẫu nhiên giữa một mức tối thiểu và tối đa được chỉ định (bao gồm). Bạn có thể sử dụng
>>> print("rock" == "Rock")
False
4 để giúp tìm ra giới hạn trên nên có trong mã của bạn:

Hướng dẫn what is rock paper scissors game in python? - trò chơi oẳn tù tì trong python là gì?

Bởi vì các giá trị

import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4 bắt đầu đếm từ
>>> print("rock" == "Rock")
False
0 và Len () bắt đầu đếm từ
>>> print("rock" == "Rock")
False
7, điều quan trọng là phải làm
>>> print("rock" == "Rock")
False
8.handle the exception.

Khi bạn kiểm tra điều này, đã giành được một lời nhắc. Nó chỉ đơn giản là trả về hành động được liên kết với số ngẫu nhiên:

print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
2

Trông được! Tiếp theo, bạn cần một cách để xác định người chiến thắng. Hàm này sẽ có hai đối số, hành động của người dùng và hành động máy tính. Nó không cần phải trả lại bất cứ điều gì vì nó sẽ chỉ hiển thị kết quả cho bảng điều khiển:

Điều này khá giống với so sánh đầu tiên bạn sử dụng để xác định người chiến thắng. Bây giờ bạn chỉ có thể so sánh trực tiếp các loại import random while True: user_action = input("Enter a choice (rock, paper, scissors): ") possible_actions = ["rock", "paper", "scissors"] computer_action = random.choice(possible_actions) print(f"\nYou chose {user_action}, computer chose {computer_action}.\n") if user_action == computer_action: print(f"Both players selected {user_action}. It's a tie!") elif user_action == "rock": if computer_action == "scissors": print("Rock smashes scissors! You win!") else: print("Paper covers rock! You lose.") elif user_action == "paper": if computer_action == "rock": print("Paper covers rock! You win!") else: print("Scissors cuts paper! You lose.") elif user_action == "scissors": if computer_action == "paper": print("Scissors cuts paper! You win!") else: print("Rock smashes scissors! You lose.") play_again = input("Play again? (y/n): ") if play_again.lower() != "y": break 4 mà không phải lo lắng về những chuỗi phiền phức đó!

Bạn thậm chí có thể kiểm tra điều này bằng cách chuyển các tùy chọn khác nhau cho

ROCK_ACTION = 0
PAPER_ACTION = 1
SCISSORS_ACTION = 2
0 và xem những gì được in:

Hướng dẫn what is rock paper scissors game in python? - trò chơi oẳn tù tì trong python là gì?

Vì bạn có thể tạo ra một hành động từ một số, điều gì sẽ xảy ra nếu người dùng của bạn cố gắng tạo hành động từ

ROCK_ACTION = 0
PAPER_ACTION = 1
SCISSORS_ACTION = 2
1? Hãy nhớ rằng, số lớn nhất mà bạn đã xác định cho đến nay là
>>> print("rock" == "Rock")
False
1:

Rất tiếc! Bạn không muốn điều đó xảy ra. Trường hợp trong sơ đồ bạn có thể thêm một số logic để đảm bảo người dùng đã nhập một lựa chọn hợp lệ?key-value relationship. In this case, the key can be an action, like scissors, and the value can be a list of actions that it beats.

Thật hợp lý khi bao gồm kiểm tra ngay sau khi người dùng đưa ra lựa chọn của họ:

possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
9

Nếu người dùng nhập một giá trị không hợp lệ, thì bạn lặp lại bước để có được sự lựa chọn của người dùng. Yêu cầu thực sự duy nhất cho lựa chọn người dùng là nó có giữa

>>> print("rock" == "Rock")
False
0 và
>>> print("rock" == "Rock")
False
1, bao gồm. Nếu đầu vào của người dùng nằm ngoài phạm vi này, thì ngoại lệ
ROCK_ACTION = 0
PAPER_ACTION = 1
SCISSORS_ACTION = 2
5 sẽ được nâng lên. Để tránh hiển thị thông báo lỗi mặc định cho người dùng, bạn có thể xử lý ngoại lệ.

print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
4

Bây giờ bạn đã xác định một vài chức năng phản ánh các bước trong sơ đồ của bạn, logic trò chơi của bạn có tổ chức và nhỏ gọn hơn rất nhiều. Đây là tất cả các vòng lặp

print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
9 của bạn cần phải chứa ngay bây giờ:

Vì bạn không còn sử dụng các tuyên bố dài

if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")
3 ____ ____34 ____ ____35, nên nó tương đối không đau khi thêm séc cho những hành động mới này. Bạn có thể bắt đầu bằng cách thêm Lizard và Spock vào
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4:

print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
5

Tiếp theo, thêm tất cả các mối quan hệ chiến thắng từ sơ đồ. Đảm bảo làm điều này bên dưới

import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4 để
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
09 có thể tham khảo mọi thứ trong
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4:

print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
6

Lưu ý làm thế nào bây giờ mỗi

import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4 có một danh sách chứa hai yếu tố mà nó đánh bại. Trong quá trình thực hiện kéo giấy đá cơ bản, chỉ có một yếu tố.

Bởi vì bạn cố tình viết

>>> play_again = "yes"
>>> play_again == "n"
False
>>> play_again != "y"
True
3 để phù hợp với các hành động mới, bạn không phải thay đổi bất cứ điều gì về mã đó. Điều tương tự cũng đúng với
user_input = input("Enter a choice (rock[0], paper[1], scissors[2]): ")
user_action = int(user_input)
if user_action == ROCK_ACTION:
    # Handle ROCK_ACTION
1. Vì độ dài của
import random

while True:
    user_action = input("Enter a choice (rock, paper, scissors): ")
    possible_actions = ["rock", "paper", "scissors"]
    computer_action = random.choice(possible_actions)
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break
4 đã thay đổi, phạm vi của các số ngẫu nhiên cũng sẽ thay đổi.

Hãy nhìn vào mã ngắn hơn và dễ quản lý hơn bây giờ! Để xem mã đầy đủ cho chương trình hoàn chỉnh của bạn, hãy mở rộng hộp bên dưới.

print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
7

Đó là nó! Bạn đã triển khai Lizard Spock của Rock Paper Scissors trong mã Python. Kiểm tra kỹ để chắc chắn rằng bạn đã không bỏ lỡ bất cứ điều gì và cho nó một cách chơi.

Sự kết luận

Xin chúc mừng! Bạn vừa hoàn thành trò chơi Python đầu tiên của bạn! Bây giờ bạn đã biết cách tạo ra kéo giấy đá từ đầu và bạn có thể mở rộng số lượng hành động có thể trong trò chơi của bạn với nỗ lực tối thiểu.

Trong hướng dẫn này, bạn đã học được cách:

  • Mã trò chơi kéo giấy đá của riêng bạnrock paper scissors game
  • Đưa vào đầu vào của người dùng với
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
    
    8
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
    
    8
  • Chơi một số trò chơi liên tiếp bằng cách sử dụng vòng lặp
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
    
    9
    print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
    
    9 loop
  • Làm sạch mã của bạn bằng
    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")
    
    0 và các chức năng
    if user_action == computer_action:
        print(f"Both players selected {user_action}. It's a tie!")
    elif user_action == "rock":
        if computer_action == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif user_action == "paper":
        if computer_action == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif user_action == "scissors":
        if computer_action == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")
    
    0
    and functions
  • Mô tả các quy tắc phức tạp hơn với một từ điểndictionary

Những công cụ này sẽ tiếp tục giúp bạn trong suốt nhiều cuộc phiêu lưu lập trình của bạn. Nếu bạn có bất kỳ câu hỏi nào, thì hãy thoải mái tiếp cận trong phần bình luận bên dưới.

Kéo giấy đá trong Python là gì?

Đối tượng của dự án Python-Scissor Python là xây dựng một trò chơi cho một người chơi chơi với máy tính, bất cứ nơi nào và mọi lúc. Dự án này dựa trên các quy tắc mà: Rock Blunts kéo nên rock thắng. Kéo cắt giấy để thắng.build a game for a single player that plays with a computer, anywhere, and anytime. This project is base on the rules that: rock blunts scissors so rock wins. scissors cut the paper so scissors win.

Mục đích của trò chơi kéo giấy đá là gì?

Kéo giấy đá thường được sử dụng như một phương pháp lựa chọn công bằng giữa hai người, tương tự như lật đồng xu, vẽ ống hút hoặc ném xúc xắc để giải quyết tranh chấp hoặc đưa ra quyết định nhóm không thiên vị.a fair choosing method between two people, similar to coin flipping, drawing straws, or throwing dice in order to settle a dispute or make an unbiased group decision.

Làm thế nào để bạn tạo ra một trò chơi kéo giấy đá với số điểm trong Python?

Thuật toán cho trò chơi cắt tỉa giấy đá..
Lập danh sách 3 tùy chọn- đá, giấy, cắt kéo ..
Hỏi tên của người chơi để chúng tôi có thể hiển thị nó trên bảng điểm ..
Khởi tạo điểm số của người chơi và máy tính thành 0 và số vòng đến 0 ..
Đặt cờ Gameon thành True ..
Trong khi cờ Gameon là đúng các bước 6 đến 12 ..

Trò chơi kéo giấy đá được gọi là gì?

Roshambo hoặc kéo giấy đá..