1

I'm wanting to write code that takes bowling scores and totals/averages them, so if user inputs a number accidentally over 300 it will not store it/accept it and continue the loop for another valid score entry and also if user presses Enter accidentally before input it won't crash and give Error. Right now it's able to store the numbers input but still accepts numbers over 300, which I don't want it to do, just want it not storing them and continuing loop to accept a valid number (0-300)

# stores a set of numbers
nums = []
scores = ""

# While loop for user input of score
while True:
    scores = input("Please enter a score (xxx) or 'q' to exit: ")

# If input is not 'Q' or 'q', will add Integers to nums list under variable 'scores'
    if scores.lower() != 'q':
        nums.append(int(scores))

# Otherwise if 'Q' or 'q' is input, will stop loop and display 'quitting'
    elif scores.lower() == 'q':
        print("quitting")
        break

# stores 'scores' as integers and adds total list of numbers that were input for 'nums'
    scores = int()
    total = sum(nums)

# line space
print("")

# displays number of games played, total score and average
print(f"You played {len(nums)} games")
print(f"The total score is: {total}")

# finds average of scores based on "length" or amount of numbers that were input for 'nums' list and stores it as variable
average = total / len(nums)
print("The score average is: ", round(average, 2))

Error when pressing Enter:

Traceback (most recent call last):
  File "/Users/ryaber/PycharmProjects/pythonProject4/main.py", line 12, in <module>
    nums.append(int(scores))
ValueError: invalid literal for int() with base 10: ''

Looked at other examples and not sure where to implement what, thank you for any help.

Ryan B
  • 23
  • 4

1 Answers1

0

instead of while True:, why don't use something like while total<300: ?

Amur Koko
  • 11
  • 3