Python 石头剪刀布游戏代码
import random
print('欢迎来玩石头剪刀布游戏!')
print('请输入您的选择:')
print('1. 石头 2. 剪刀 3. 布')
player_choice = int(input())
if player_choice not in [1, 2, 3]:
print('请输入有效数字!')
else:
computer_choice = random.randint(1, 3)
print('电脑选择了:', computer_choice)
if player_choice == computer_choice:
print('平局!')
elif player_choice == 1 and computer_choice == 2:
print('您赢了!')
elif player_choice == 2 and computer_choice == 3:
print('您赢了!')
elif player_choice == 3 and computer_choice == 1:
print('您赢了!')
else:
print('电脑赢了!')
注意:上述代码是基于 Python 3 的,如果是 Python 2 的话需要做一些修改,比如 input() 函数应改为 raw_input() 函数。
原文地址: https://www.cveoy.top/t/topic/m0eO 著作权归作者所有。请勿转载和采集!