第一次写这东西,主要是为了更好地纪录自身的学习培训过程 ,也就是说是为了更好地忘记的情况下找回家看一下。
今天报名参加风变编程学习培训第10天。昨晚完成了第10每关底的划拳游戏 。
规定:人与电脑上轮着出拳。分辨胜负。
给出目录:punches=['石块','剪子','布']
一切正常编码以下:(风变给出的正确答案)
n = 0
while n < 10000:
import random
punches = ['石块','剪子','布']
computer_choice = random.choice(punches)
user_choice = random.choice(punches)
if user_choice == computer_choice:
print('平手!')
elif (user_choice == '石块' and computer_choice == '剪子') or (user_choice == '剪子' and computer_choice == '布') or (user_choice == '布' and computer_choice == '石块'):
print('你赢了!')
else:
print('你输了!')
import random
n = n 1
取得题型的第一个印像是,这一目录里,左边赢右边 。也就是说依照键值 ,小的为赢。让后想起‘布’的键值为2,如何让2低于0是一个难题。为了更好地处理这个问题,列举了一个Excel ,来表述这个问题:
依据组成和查询 。当电脑上挑选布的情况下,将其結果强制性表述为-1(这一在目录里是容许的)那样就考虑了左边赢右边的规定。因此我的编码写出了下边的模样:
import random
# 出拳
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('—————結果—————')
a=punches.index(computer_choice)
b=punches.index(user_choice)
if a==b:
print('此次和局')
elif a==2:
a=-1
if punches(a,b)in punches:
print('你输了。')
else:
print('你赢了 。')
与正确答案的与众不同的地方用鲜红色标示了 。是否对程序流程简单了?哈哈哈哈哈,尽管仅仅一个游戏,针对刚开始学编程的我而言還是较为有满足感的 ,终究是一个更为简单的构思。
有木有哪个盆友帮助捧场祝贺呢?第一次用这个东西,也不知道有木有关注这类。哈哈哈哈哈 。较为春风得意了。