-1

This should be the easiest thing ever, I have never felt so dumb before. I had a lot of else-if's in my code, so I attempted to change it to a match to be fancy. I have tried Python on my computer and repl (the website), I have tried example code from every website I could go to, but no matter what I try it says the syntax after "match" is wrong. The exact error for the code (pasted below) is:

File "main.py, line 5  
match cpuModel: 
     ^
SyntaxError: invalid syntax

That error was from REPL. Here is the code that is NOT MINE, however, as the error is exactly the same as my code and this is far more readable than my code, and proves that even internet example code does not work for me, I feel like this is a reasonable substitute.

# First, ask the player about their CPU
cpuModel = str.lower(input("Please enter your CPU model: "))
 
# The match statement evaluates the variable's value
match cpuModel:

case "celeron": # We test for different values and print different messages
        print ("Forget about it and play Minesweeper instead...")
case "core i3":
        print ("Good luck with that ;)")
case "core i5":
        print ("Yeah, you should be fine.")
case "core i7":
        print ("Have fun!")
case "core i9":
        print ("Our team designed nice loading screens… Too bad you won't see them...")
case _: # the underscore character is used as a catch-all.
        print ("Is that even a thing?")

This code is from https://www.udacity.com/blog/2021/10/python-match-case-statement-example-alternatives.html. I know I could use else and if's and even the amazing "elif", but I just want to understand what is going wrong for me.

The summary: It is not my code or my computer's instillation of Python, example code does not work for me and an online IDE that does not require Python to be on my computer. Both my code and online code does not have proper syntax for a "match" statement. All I am asking is for the proper syntax on a match statement. A single example that works. If it is important, I am personally running Python 3.11 as is REPL. I feel absolutely goofy asking for help on the syntax of match, but I am about to commit crimes to my bathroom.

Yash Mehta
  • 2,025
  • 3
  • 9
  • 20
Cody
  • 1
  • 1
  • 1
    The example from that website is simply wrong, and my installation of Python gives a clear error message: `expected an indented block after 'match' statement`. You need to indent all of the case statements and the code blocks within each of them. Note that this also requires a fairly new version of Python; some online IDEs may not have 3.10 yet. – nanofarad Dec 23 '22 at 04:54
  • Welcome to Stack Overflow. Please read [ask] and [mre]. Most importantly, note well that this is **not a discussion forum**; we want questions to be direct and not conversational. Do not talk about your personal mental state, level of experience, or anything else outside of the **the specific issue with the code**. Make sure to show code **that you are trying** (whether or not you wrote it) in order to cause the problem; and make sure that **copying and pasting** the code shown **without changing anything** will **directly** show the problem **exactly** as you describe. – Karl Knechtel Dec 23 '22 at 05:52
  • 1
    If you want to exhibit, for example, the behaviour in the 3.11 REPL, then type code into the 3.11 REPL, and **copy and paste** the complete session, showing us the welcome header (that confirms the version), the code you tried, and the error you got - all formatted as multi-line code. If you mean that you used the site repl.it, note that Stack Overflow is not tech support - we can't tell you about what version of Python that third-party service is using, or help with using it. – Karl Knechtel Dec 23 '22 at 05:53
  • That said, the error that you show **very definitely does not** come from Python 3.10 or higher. Please make sure you know how to verify the Python version in use. In fact, it isn't even an exact, copied and pasted error message - I can tell, because it's missing a closing quotation mark around the filename. – Karl Knechtel Dec 23 '22 at 05:55

1 Answers1

-1

You have indentation problem..

Code:-

cpuModel = str.lower(input("Please enter your CPU model: "))
Please enter your CPU model: core i3
match cpuModel:
    case "celeron":
            print ("Forget about it and play Minesweeper instead...")
    case "core i3":
            print ("Good luck with that ;)")
    case "core i5":
            print ("Yeah, you should be fine.")
    case "core i7":
            print ("Have fun!")
    case "core i9":
            print ("Our team designed nice loading screens… Too bad you won't see them...")
    case _:
            print ("Is that even a thing?")
 
Good luck with that ;)

Run on python.org online

Run on python.org online

Yash Mehta
  • 2,025
  • 3
  • 9
  • 20