以下是一段简单的猜拳游戏代码:

import random

print('欢迎来到猜拳游戏!')

定义游戏结果的字典

result = {0: '平局', 1: '你赢了!', 2: '电脑赢了!'}

while True: # 等待用户输入 user_choice = input('请出拳(1:石头;2:剪刀;3:布): ') # 验证用户输入是否正确 if user_choice not in ['1', '2', '3']: print('输入有误,请重新输入!') continue # 将用户输入转换为数字 user_choice = int(user_choice) # 生成电脑随机出拳 computer_choice = random.randint(1, 3) # 输出电脑出拳结果 print('电脑出拳:', end='') if computer_choice == 1: print('石头') elif computer_choice == 2: print('剪刀') else: print('布') # 计算游戏结果 if user_choice == computer_choice: print(result[0]) elif user_choice == 1 and computer_choice == 2 or user_choice == 2 and computer_choice == 3 or user_choice == 3 and computer_choice == 1: print(result[1]) else: print(result[2]) # 询问用户是否继续游戏 play_again = input('是否继续游戏?(y/n): ') if play_again.lower() != 'y': break

print('游戏结束,谢谢参与!')

Python 猜拳游戏代码:简单易懂,立即体验!

原文地址: https://www.cveoy.top/t/topic/oso4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录