编写一个猜拳的python代码
时间: 2024-03-02 22:31:45 浏览: 94
好的,以下是猜拳的Python代码:
import random
choices = ["rock", "paper", "scissors"]
while True:
player_choice = input("Please choose rock, paper or scissors (or type quit to exit): ")
if player_choice.lower() == "quit":
break
if player_choice.lower() not in choices:
print("Invalid choice.")
continue
computer_choice = random.choice(choices)
print("You chose", player_choice)
print("Computer chose", computer_choice)
if player_choice.lower() == computer_choice:
print("It's a tie!")
elif player_choice.lower() == "rock" and computer_choice == "scissors":
print("You win!")
elif player_choice.lower() == "paper" and computer_choice == "rock":
print("You win!")
elif player_choice.lower() == "scissors" and computer_choice == "paper":
print("You win!")
else:
print("Computer wins!")
阅读全文