简体   繁体   中英

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 :

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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