【问题标题】:Rock Paper Scissors PYTHON石头剪刀布 PYTHON
【发布时间】:2018-04-06 23:25:07
【问题描述】:

Python 石头剪刀布非常适合我。我只是有点困惑如何将“人类”使用每种武器的次数加起来并打印出来……

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")

【问题讨论】:

    标签: python loops if-statement random while-loop


    【解决方案1】:
    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"))
    

    【讨论】:

    • 给出答案很好,但如果你解释它会更好
    【解决方案2】:

    您可以使用带有武器的字典作为键,每次选择它们时都会更新。字典将在 while 循环之前以 0 个计数预先初始化,

    human_choices = {'Rock': 0, 'Paper': 0, 'Scissors': 0};
    

    然后您可以在选中 Quit 选项后更新 elif 中的条目:

    elif human in rules:
            human_choices[human]+=1
            previous.append(human)
    

    您可以在 Quit if 中打印计数:

        print(human_choices)
    

    附带说明,如果您希望程序在输入Quit 后真正结束,您需要在 Quit-if 的末尾添加 break 语句:

        print("SeE YoU LaTeR!!! :)")
        break
    

    【讨论】:

    • 如果你能提供帮助,我认为你不应该从你的代码中调用exit(0)。如果出现问题,它主要用于设置退出代码 (exit(1))
    • @Adam Smith 谢谢,我删除了它。尽管如果您认为这样做很好,我可以包括您的评论。通常我会使用break,但我认为在那个位置退出可能会有一些好处,因为while循环似乎是代码的逻辑结束。
    【解决方案3】:

    这是你要找的吗?:

    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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多