接上个问题,对手是电脑,电脑采用 随机出拳 —— 先假定电脑只会出石头,完成整体代码功能,然后比较胜负
时间: 2024-04-10 12:28:39 浏览: 13
好的,我们可以继续完善代码,使其与电脑进行比较,并判断胜负。以下是完整的代码示例:
```python
import random
def get_user_choice():
user_input = input("请出拳:石头(1)/剪刀(2)/布(3):")
while user_input not in ["1", "2", "3"]:
user_input = input("无效的选择,请重新出拳:石头(1)/剪刀(2)/布(3):")
return int(user_input)
def get_computer_choice():
return 1 # 假设电脑只会出石头
def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "平局"
elif (user_choice == 1 and computer_choice == 2) or (user_choice == 2 and computer_choice == 3) or (user_choice == 3 and computer_choice == 1):
return "你赢了!"
else:
return "电脑赢了!"
# 获取用户输入
user_choice = get_user_choice()
# 获取电脑选择
computer_choice = get_computer_choice()
# 判断胜负
result = determine_winner(user_choice, computer_choice)
# 打印结果
if result == "平局":
print("平局")
else:
print(f"{result}恭喜!")
```
运行上述代码,程序将提示您输入要出的拳,然后与电脑的选择进行比较,并输出胜负结果。由于我们假设电脑只会出石头,因此无论您出什么拳,结果都会是平局。
相关推荐
![flv](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)