python 猜拳代码
时间: 2023-04-10 16:03:57 浏览: 96
以下是一个简单的 Python 猜拳游戏代码:
import random
print("Welcome to the Rock-Paper-Scissors game!")
print("You will be playing against the computer.")
choices = ["rock", "paper", "scissors"]
while True:
player_choice = input("Please choose rock, paper, or scissors: ").lower()
if player_choice not in choices:
print("Invalid choice. Please try again.")
continue
computer_choice = random.choice(choices)
print(f"The computer chose {computer_choice}.")
if player_choice == computer_choice:
print("It's a tie!")
elif player_choice == "rock" and computer_choice == "scissors":
print("You win!")
elif player_choice == "paper" and computer_choice == "rock":
print("You win!")
elif player_choice == "scissors" and computer_choice == "paper":
print("You win!")
else:
print("You lose!")
play_again = input("Do you want to play again? (y/n) ").lower()
if play_again != "y":
break
注意:这只是一个简单的猜拳游戏代码,可能存在一些问题,如有需要请自行修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)