电脑
Python 软件
打开Python 软件,新建Python files
输入代码如下:import random,time punches = ['石头','剪刀','布'] computer_choice = random.choice(punches) while True: user_choice='' user_choice=input('请出拳:(剪刀,石头,布)') while user_choice not in punches: print('输入错误,请重新出拳:') user_choice=input() print('----战斗过程----') print(computer_choice) print(user_choice) time.sleep(1) if (computer_choice=='石头' and user_choice=='布') or (computer_choice=='布' and user_choice=='剪刀') or (computer_choice=='剪刀' and user_choice=='石头'): print('yeah,你赢了!') break elif computer_choice==user_choice: continue else: print('Oh,你输了') break
1.点击图片所示红色框运行按钮,运行程序
分别测试几种输出的不同 结果1.输入‘石头’,电脑出‘布’,结果是你输了2.输入‘one punch’,错误指令,提示重新输入,重新输入‘石头’,电脑出‘布’,结果是你赢了3.输入‘石头‘,电脑出’石头‘,打和了,但是打和是不存在的,还是要决一胜负的,提示重新出拳,重新输入’布‘,电脑出’石头‘,终于决出结果,你赢了!
程序的格式是有严格要求的,不能随便调位置,否则运行会出错!