|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码如下,问题在最后:
- #和电脑玩猜拳游戏
- import random
- while True:
- # 出拳
- punches = ['石头','剪刀','布']
- computer_choice = random.choice(punches)
- user_choice = ''
- user_choice = input('请出拳:(石头、剪刀、布)') # 请用户输入选择
- while user_choice not in punches: # 当用户输入错误,提示错误,重新输入
- print('输入有误,请重新出拳')
- user_choice = input()
- # 亮拳
- print('————战斗过程————')
- print('电脑出了:%s' % computer_choice)
- print('你出了:%s' % user_choice)
- # 胜负
- print('—————结果—————')
- if user_choice == computer_choice: # 使用if进行条件判断,直接相等就得到平局的结果
- print('平局!')
- elif user_choice == punches[punches.index(computer_choice)-1]:
- print('你赢了!');
- else:
- print('你输了!')
- xh = input('n退出,其余键继续')
- if xh == "n":
- print('程序结束')
- break
复制代码
我想问下 elif user_choice == punches[punches.index(computer_choice)-1]:这里为什么不能把最后的-1改为+1?
+1的情况下,电脑随机到布就会报错?
-1的情况下,电脑随机到石头,却没问题?
求大神指教,先谢过
列表中下标 -1 表示最后一个元素
使用 +1 会使列表下标越界,产生异常
|
|