-2

Requirements: Your program should provide a menu driven interface in which the user can do the following:

  1. Choose to exit
  2. Choose to display the scores from highest to lowest values.
  3. Choose to add a score to the list. Note that scores must be floating point numbers.
  4. Choose to display the highest and lowest scores.

I am a definitely a novice at coding and very lost on what to do.

menu = """
1: Exit
2: List scores so far
3: Add a score
4: Display the highest and lowest scores
"""    


list1 = ['85.3','85.2','21.99']

done = False
while not done:

    print()
    print()
    print("Scoring Engine")
    print()
    print(menu)
    selection = input("Please enter a selection between 1 and 4: ")
    print()


    if selection == "1": #exit
        done = True

    elif selection == "2": #list scores so far
        list1 = [float(i) for i in list1]
        list1.sort(reverse=True)
        print("Scores recorded so far: ")
        print()
        print(*list1, sep="\n")

    elif selection == "3": #adds a score
        print()
        new_score = input("Please enter a score between 0 and 100: ")
        try:
            new_score = float(new_score)
        except ValueError:
            print()
            print("Please enter a valid score between 0 and 100.")
            continue
        if float(new_score)<0:
            print()
            print("{:.2f} is too low. Scores must be between 0 and 100".format(new_score))
        elif float(new_score)>100:
            print()
            print("{:.2f} is too high. Scores must be between 0 and 100".format(new_score))
        else:
            list1.append(new_score)
            print("Score added")

Problem: When I add a score and then go back to option 2 I get:

Traceback (most recent call last):
  File "C:\Users\******\Desktop\hwk4.py", line 36, in <module>
list1.sort(reverse=True)
TypeError: '<' not supported between instances of 'str' and 'float'

Is there a way to get the appended values to be sorted when selecting option 2?

1 Answers1

0

As written, the line [float(i) for i in list1] creates a list of floating point numbers and throws it away. You just need to add list1 = so you'll have list1 = [float(i) for i in list1].

Alternatively, you could define the list as floating point numbers in the first place: list1 = [85.3, 85.2, 21.99].

Also you should probably put your existing code inside a loop (perhaps a while loop), so that after it obeys the user's choice, it should go back to the menu and let the user make another choice.

krubo
  • 5,969
  • 4
  • 37
  • 46
  • This worked great! Thank you. Now I have the issue of in option 2 when the list prints, I would like to have it add 0's or shorten an appended value to 2 decimal places. I'm not sure if you would like to explain how that would work as well. – d0ppler302 Aug 11 '19 at 03:53
  • See if this helps: https://stackoverflow.com/questions/2389846/python-decimals-format – krubo Aug 11 '19 at 17:26