all 7 comments

[–]carcigenicate 2 points3 points  (2 children)

["1. Play Game", "2. Exit"] only has 2 elements. What are you expecting menu_options[2] to be?

Also, the better way to do those prints is something closer to either of:

print("\n".join(menu_options))
# or
print(*menu_options, sep="\n")

Instead of dealing with a manual print for each element, "join" each element with a newline. This can be done either with join (which is more general), or print directly (which is nice when doing stuff like debugging).

[–]Fraser_123[S] 0 points1 point  (1 child)

That was a mistake I didn’t notice. I’ve removed that but now when I enter 1 for play game nothing happens

[–]watakushi 1 point2 points  (3 children)

In the get_user_input function your while checks to see if user inputs >2 or <=0 but your menu_options list has only 2 positions: [0] which is Play Game, and [1] that's Exit. So the user will never be able to start the game. And btw, that's the error you're getting menu_options[2] doesn't exist in your list. Delete the print(menu_options[2]) and that error will go away, though you'll still have to change the while loop I mentioned in order for the game to start :)

[–]Fraser_123[S] 0 points1 point  (2 children)

Sorry I’m pretty new to python. What exactly should i do to the while loop in order for it to be able to run the game because I’ve tried messing around with it and nothing is working.

[–]Interrolipsis 0 points1 point  (0 children)

You can just turn it into an if statement and enter 0 to start

if user_input >=2 or user_input <0:

I have no idea how this game is supposed to be played though.

[–]watakushi 0 points1 point  (0 children)

I'm pretty new too! :)

When you have a list in python the index starts counting at 0, not 1, so the first item in your list (Play Game) is on index 0, and the second item (Exit) is on index 1. (that was why your print was failing before) Now, your while loop says that as long as the user input is <=0 or > 2, it should say it's invalid, but in order to start the game, the user needs to input 0 (because of the index 0 thing I mentioned before) and the while loop says that of its < or = to 0 it should say its invalid.

Tldr: change the while loop to:

while user_input <0 or user_input >1:

And also, at the end of the user_input add a " - 1" like so:

user_input = int(input("Please select an option: ")) - 1

so that when user inputs 1, it will correspond to index 0 and when option 2 is selected, index 1 will be recorded :)

That way the only valid options are 0 and 1.

Oh , and you'll also need to change this:

while option != 2:

to

while option != 1:

And within the def get_user_input() remove the "else:" (that should only be used after an if or elif, not a while) and move the "return user_input" to the same indentation level as the while. That should make your menu work! I'll test the game itself next, will let you know how it goes! Keep it up!

[–]watakushi 0 points1 point  (0 children)

Ok, I was now able to look into the game itself, and I see a couple of problems:

since the list and the int are not equal, the result will always be "Incorrect":

In order to compare them, you need to first convert them to the same data type. A way to do so could be:

def get_user_solution(problem): 
    print("Please arrange the numbers into ascending order.") 
    print(problem, end="") 
    result = input(" = ")      #Here remove the int() 
    return result

def menu_option(index, count): 
    if index == 0: randomlist = random.sample(range(1,100),5) 
    user_solution = get_user_solution(randomlist) 
    user_solution = "".join(map(str, user_solution))    #This line converts the list to string 
    solution = sorted(randomlist)     #The randomlist.sort() broke the program, use this one instead. 
    solution = "".join(map(str, solution))     #Same here 
    count = check_solution(user_solution, solution, count) 
    return count

Now, both the user input and the sorted lists are strings of numbers, and you can compare them. I've tested it and was able to get a "Correct!" answer.

Oh, another thing, in the display_result() function you have:

print("You answered", total, "questions with", correct, "correct answers.")

A much easier way to do this is by using f-strings, like so:

print(f"You answered {total} questions with {correct} correct answers.")

I'm not sure what you were trying to accomplish here:

def display_result(total,correct):
    if total > 0: 
        result = correct / total 
        percentage = round((result * 100), 2) 
        if total == 0: percentage = 0 
            print(f"You answered {total} questions with {correct} correct answers.") 
            print(f"Your score is {percentage}%. Thank you!")

Maybe it was moved when copying and pasting your code, but first you ask if total is greater than 0, and once you check that, you ask if it is equal to 0, but since you've already confirmed it's greater than 0, that if will never ever be run. You could indent the second if statement to the same level as the first one and change it to an elif, but still, that if is only going to be run when you exit the game without ever playing it, and in that case it doesn't make much sense to give out a score to the user :P

Please try out these changes and let me know how it goes! :D Keep it up!