简体   繁体   English

显示每个输出在“X”中显示的次数

[英]Display the number of times each output was displayed in 'X's

while True:
    try:
        wins = int(input("enter number of wins: "))                
        draws = int(input("enter number of draws: "))
        losses = int(input("enter number of losses: "))
    except ValueError:
        print('try again')
        continue

    if wins>3:
        print('qualify')
    elif losses>3:
       print('disqualify')
   elif draws>3:
       print('try again')

   restart = str(input('enter "restart" to restart or "quit" to quit'))
   if restart == 'restart':
       continue
   elif restart == 'quit':
       #quit and display number of occurrences of qualify, disqualify and try again in 'X's 

I didn't understand your question but based on what I understood I constructed a solution.我不明白你的问题,但根据我的理解,我构建了一个解决方案。 This will show you the result of occurrence of qualify and disqualify这将向您显示合格和不合格发生的结果

qualifycount=0
disqualifycount=0
while True:
    try:
        wins = int(input("enter number of wins: "))                
        draws = int(input("enter number of draws: "))
        losses = int(input("enter number of losses: "))
    except ValueError:
        print('try again')
        continue

    if wins>3:
        print('qualify')
        qualifycount+=1
    elif losses>3:
       print('disqualify')
       disqualifycount+=1
    elif draws>3:
       print('try again')
   
    restart = str(input('enter "restart" to restart or "quit" to quit'))
    if restart == 'restart':
       continue
    elif restart == 'quit':
       #quit and display number of occurrences of qualify, disqualify and try again in 'X's
       print("Number of qualify:"+str(qualifycount))
       print("Number of disqualify:"+str(disqualifycount))
       restart= str(input('press X to run again or press enter to quit'))
       if restart=='X':
           continue
       else:
           break


First you need to track the count of each result.首先,您需要跟踪每个结果的计数。 You could do this with three different variables, but I think it's a little easier to use a dictionary, or better yet a Counter :你可以用三个不同的变量来做到这一点,但我认为使用字典更容易一些,或者更好的是Counter

from collections import Counter

counts = Counter()

Make sure to initialize this outside your loop, since you want to keep the same Counter through multiple iterations of the loop;确保循环初始化它,因为您希望通过循环的多次迭代保持相同的计数器; if you reset it inside the loop, it won't keep track of anything!如果您在循环内重置它,它将不会跟踪任何内容!

Then track the results as you see them:然后跟踪您看到的结果:

    if wins > 3:
        result = "qualify"
    elif losses > 3:
        result = "disqualify"
    elif draws > 3:
        result = "try again"
    else:
        print("Not enough of anything to produce a result!")
        continue
    print(result)
    counts[result] += 1

and then at the end, you can convert the counts into "X" marks by string multiplication:然后最后,您可以通过字符串乘法将计数转换为“X”标记:

for result in counts:
    print(f"{result}: {'X' * counts[result]}")

All together it looks like:放在一起看起来像:

from collections import Counter

counts = Counter()
while True:
    try:
        wins = int(input("enter number of wins: "))                
        draws = int(input("enter number of draws: "))
        losses = int(input("enter number of losses: "))
    except ValueError:
        print("try again")
        continue

    if wins > 3:
        result = "qualify"
    elif losses > 3:
        result = "disqualify"
    elif draws > 3:
        result = "try again"
    else:
        print("Not enough of anything to produce a result!")
        continue
    print(result)
    counts[result] += 1

    if str(input('enter "restart" to restart or "quit" to quit')) == "quit":
        break

for result in counts:
    print(f"{result}: {'X' * counts[result]}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM