猜拳游戏,怎么能让代码更简洁一些……
import easygui as gimport sys
import random
def bet(computer_choice,choice):
if computer_choice != choice:
if computer_choice=='石头' and choice=='剪刀':
result='电脑赢了'
elif computer_choice=='剪刀' and choice=='布':
result='电脑赢了'
elif computer_choice=='布' and choice=='石头':
result='电脑赢了'
else:
result='你赢了'
else:
result='平局'
return result
while True:
num=random.randint(1,3)
if num==1:
computer_choice='石头'
elif num==2:
computer_choice='剪刀'
elif num==3:
computer_choice='布'
g.msgbox('开始猜拳游戏')
msg='请问你希望出什么呢?'
title='小游戏互动'
choices=['石头','剪刀','布']
choice=g.choicebox(msg,title,choices)
g.msgbox('你的选择是:'+str(choice)+'\n'+'电脑的选择是:'+str(computer_choice) +'\n'+ '\t'+ bet(computer_choice,choice)+'!!!','结果')
msg='你希望重新开始猜拳游戏吗?'
title='请选择'
if g.ccbox(msg,title):
pass
else:
sys.exit(0)
import easygui as g
import random
def bet(computer_choice,choice):
if computer_choice-choice == -1 or computer_choice-choice == 2:
result='电脑赢了'
elif computer_choice-choice == 0:
result='平局'
else:
result='你赢了'
return result
RPS=['石头','剪刀','布']
while True:
num=random.randint(1,3)
g.msgbox('开始猜拳游戏')
msg='请问你希望出什么呢?'
title='小游戏互动'
uchoice=g.indexbox(msg,title,choices=RPS)
g.msgbox('你的选择是:%s\n电脑的选择是:%s\n\t%s!!!'% (RPS, RPS,bet(num-1,uchoice)),'结果')
msg='你希望重新开始猜拳游戏吗?'
title='请选择'
if not g.ccbox(msg,title):
break txxcat 发表于 2021-4-26 18:05
谢谢大佬!受教了
页:
[1]