Skip to content
Advertisement

Wanting to input bowling scores, total, average them

Need for a user to input bowling scores and store them, to be totaled and averaged later and they can press “q” or “Q” to quit and total/average them. I’ve tried fumbling around figuring out loops but not sure how to deal with inputting integers and also accepting “Q” to stop loop. Thanks for any help.

nums = []
scores = ""

while scores != 'q'.lower():
    scores = input("Please enter a score (xxx) or 'q' to exit: ")
    if scores != 'q':
        nums.append(int(scores))

    elif scores == 'q'.lower():
        print("quitting")
        break
    scores = int()
    def Average(nums):
        return sum(nums) / len(nums)
    total = sum(nums)
print(f"The total score is: {total}")
average = Average(nums)
print("The score average is: ", round(average, 2))

Getting error: Traceback (most recent call last): File “/Users/ryaber/PycharmProjects/pythonProject/main.py”, line 11, in scores = input(“Please enter a score (xxx) or ‘q’ to exit: “) File “”, line 1, in NameError: name ‘q’ is not defined

So not sure how to allow it to accept “Q” to stop loop and total/average them.

Advertisement

Answer

  1. Remove .lower() in q.lower() and move it and make it to score.lower(). So if user types in q or Q both will be accepted as quit

  2. You do not need average function or while scores != 'q' instead you could change it to something simple

So complete code –

nums = []
scores = ""

while True:
    scores = input("Please enter a score (xxx) or 'q' to exit: ")
    if scores.lower() != 'q':
        nums.append(int(scores))

    elif scores.lower() == 'q':
        print("quitting")
        break
    scores = int()
    total = sum(nums)
    
print(f"The total score is: {total}")
average = total/len(nums)
print("The score average is: ", round(average, 2))

Result:

Please enter a score (xxx) or 'q' to exit: 25
Please enter a score (xxx) or 'q' to exit: 32
Please enter a score (xxx) or 'q' to exit: Q
quitting
The total score is: 57
The score average is:  28.5

Edit – While this may be complicated, this will solve your problem –

nums = []
scores = ""

while True:
    scores = input("Please enter a score (xxx) or 'q' to exit: ")
    try: # This will run when scores is a number
        scores = int(scores)

        if scores.lower() == type(0):
            nums.append(int(scores))

    except: # Will run when it is a alphabet

        if scores.lower() == 'q':
            print("quitting")
            break

        else:
            continue

    scores = int()
    total = sum(nums)
    
    print(f"The total score is: {total}")
    average = total/len(nums)
    print("The score average is: ", round(average, 2))

    break
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement