代码生成(石头剪刀布三局两胜)
时间: 2023-09-28 17:12:49 浏览: 51
好的,请看下面的代码:
```python
import random
while True:
user_wins = 0
computer_wins = 0
options = ['rock', 'paper', 'scissors']
while user_wins < 2 and computer_wins < 2:
user_choice = input("Rock, paper or scissors? ").lower()
if user_choice not in options:
print("Invalid input. Please try again.")
continue
computer_choice = random.choice(options)
print(f"\nYou chose {user_choice}, computer chose {computer_choice}.\n")
if user_choice == computer_choice:
print("It's a tie!")
elif user_choice == "rock" and computer_choice == "scissors":
print("You win!")
user_wins += 1
elif user_choice == "paper" and computer_choice == "rock":
print("You win!")
user_wins += 1
elif user_choice == "scissors" and computer_choice == "paper":
print("You win!")
user_wins += 1
else:
print("Computer wins!")
computer_wins += 1
if user_wins == 2:
print("Congratulations, you won!")
else:
print("Sorry, you lost. Better luck next time!")
play_again = input("Do you want to play again? (y/n) ").lower()
if play_again != 'y':
break
```
请将以上代码复制到 Python 编辑器中运行,然后你就可以和计算机玩石头剪刀布了,先手出拳。