编写一段锤子剪刀布的python脚本
import random
options = ['锤子', '剪刀', '布']
def get_computer_option(): return random.choice(options)
def get_user_option(): while True: user_input = input('请输入您的选项(锤子/剪刀/布):') if user_input in options: return user_input else: print('输入错误,请重新输入。')
def get_game_result(user_option, computer_option): if user_option == computer_option: return '平局' elif user_option == '锤子' and computer_option == '剪刀': return '您赢了' elif user_option == '剪刀' and computer_option == '布': return '您赢了' elif user_option == '布' and computer_option == '锤子': return '您赢了' else: return '电脑赢了'
def play_game(): computer_option = get_computer_option() user_option = get_user_option() result = get_game_result(user_option, computer_option) print('电脑选择了:%s' % computer_option) print('结果:%s' % result)
play_game()
原文地址: https://www.cveoy.top/t/topic/4oA 著作权归作者所有。请勿转载和采集!