Python 石头剪刀蟒蛇
巨蟒石剪刀很适合我。我只是有点困惑于如何计算和打印人类使用每种武器的次数Python 石头剪刀蟒蛇,python,loops,if-statement,random,while-loop,Python,Loops,If Statement,Random,While Loop,巨蟒石剪刀很适合我。我只是有点困惑于如何计算和打印人类使用每种武器的次数 from random import choice win = 0 loss = 0 tie = 0 rules = {'Rock': 'Paper', 'Scissors': 'Rock', 'Paper': 'Scissors'} previous = ['Rock', 'Paper', 'Scissors'] while True: human = input('Rock, Paper, Scissors
from random import choice
win = 0
loss = 0
tie = 0
rules = {'Rock': 'Paper', 'Scissors': 'Rock', 'Paper': 'Scissors'}
previous = ['Rock', 'Paper', 'Scissors']
while True:
human = input('Rock, Paper, Scissors or Quit???: ')
computer = rules[choice(previous)]
if human in ('Quit'):
print("YoU WoN %d TiMEs!" % win)
print("yOu lOSt %d tImEs!" % loss)
print("YoU TIeD %d TiMEs!" % tie)
print("SeE YoU LaTeR!!! :)")
elif human in rules:
previous.append(human)
print('tHe CoMPuTeR PlAyEd', computer, end='; ')
if rules[computer] == human:
print('YoU WiN!')
win += 1
elif rules[human] == computer:
print('ThE CoMpUtER BeAT YOU!!!')
loss += 1
else:
print("It'S A tIE!")
tie += 1
else: print("that's not a valid choice")
这就是你要找的吗
from random import choice
win = 0
loss = 0
tie = 0
humanRocks = 0
humanScissors = 0
humanPapers = 0
rules = {'Rock': 'Paper', 'Scissors': 'Rock', 'Paper': 'Scissors'}
previous = ['Rock', 'Paper', 'Scissors']
while True:
human = input('Rock, Paper, Scissors or Quit???: ')
computer = rules[choice(previous)]
if human in ('Quit'):
print("YoU WoN %d TiMEs!" % win)
print("yOu lOSt %d tImEs!" % loss)
print("YoU TIeD %d TiMEs!" % tie)
print("You used Rocks %d times" % humanRocks)
print("You used Paper %d times" % humanPapers)
print("You used Scissors %d times" % humanScissors)
print("SeE YoU LaTeR!!! :)")
exit(0)
elif human in rules:
previous.append(human)
print('tHe CoMPuTeR PlAyEd', computer, end='; ')
if rules[computer] == human:
print('YoU WiN!')
win += 1
elif rules[human] == computer:
print('ThE CoMpUtER BeAT YOU!!!')
loss += 1
else:
print("It'S A tIE!")
tie += 1
if human == "Rock":
humanRocks+=1
elif human == "Paper":
humanPapers+=1
elif human == "Scissors":
humanScissors+=1
else: print("that's not a valid choice")
你可以使用字典,将武器作为每次选择时都会更新的密钥。字典将在while循环之前预先初始化,计数为0
human_choices = {'Rock': 0, 'Paper': 0, 'Scissors': 0};
选中“退出”选项后,您可以更新elif中的条目:
elif human in rules:
human_choices[human]+=1
previous.append(human)
在以下情况下,您可以在退出中打印计数:
print(human_choices)
作为旁注,如果您希望您的程序在输入Quit后真正结束,您需要在Quit末尾添加break语句,如果:
print("SeE YoU LaTeR!!! :)")
break
如果可以的话,我认为您不应该从代码中调用exit0。它对于在出现问题时设置退出代码非常有用exit1@Adam史密斯:谢谢,我把它拿走了。虽然如果你认为这样做很好的话,我可以把你的评论包括在内。通常我会使用break,但我认为在那个位置退出可能有一些好处,因为while循环似乎是代码的逻辑结尾。给出答案是好的,但如果你解释一下,会更好
import random
c1=[0];p1=[0];t=1
result={-1:p1,-2:c1,1:c1,2:p1,0:[0]}
opt=["exit","Rock","Paper","Scissors",]
while 1:
print("\n","-"*30,sep="")
comp= random.randint(1,3)
user=input("Choose Option\n1. %s 2. %s 3. %s 0. %s\n>>"%(opt[1],opt[2],opt[3],opt[0]))
if user not in ['1','2','3']:break
user=int(user)
print("Comp--> %s -- %s <--You"%(opt[comp],opt[user]))
g=result[comp-user]
g[0]+=1
print("\n---Score---\nComp--> %s\nYou--> %s\n----------"%(c1[0],p1[0]))
print("\n------------------\n %s won!\n------------------"%("Computer" if c1[0]>p1[0] else "You"))