0

Hi everyone why does it give error?

https://sololearn.com/compiler-playground/c436DxiiU78C/?ref=app Note when I tried elif it worked well.

18th Feb 2024, 7:15 PM
Yusof
Yusof - avatar
16 ответов
+ 4
import sys print(sys.version) # What do you observe? # What does it tell you about your present issue?
18th Feb 2024, 7:49 PM
Lisa
Lisa - avatar
+ 3
match...case it is used in the newer version of python 3.10.
18th Feb 2024, 9:49 PM
Solo
Solo - avatar
+ 3
Yusof , Sololearn apparently upgraded from 3.9.16 to 3.9.18 five days ago, but it's still too low to use the match compound statement. If you're on Android, as I am, you can download Pydroid 3 from the Google Play Store. It's a Python 3 IDE for Android. It's free with ads, or trial subscription becomes subscription (don't start that), or pay once for life (better deal). The ads don't play in airplane mode. Pydroid 3 is currently running Python 3.11.4. I've experimented with match there.
19th Feb 2024, 12:15 AM
Rain
Rain - avatar
+ 1
"Match" is similar (not equal) not the "switch" in other language. https://docs.python.org/3/tutorial/controlflow.html#tut-match In the given code, "n" is a string, while "n.endswith(...)" is a boolean. The case values would need to be of the same type as the match value.
20th Feb 2024, 9:14 AM
Lisa
Lisa - avatar
+ 1
Yusof , Here's a refactoring that does the same job, but I won't be satisfied until I also understand why, match True: doesn't fix the TypeError in your first version. https://sololearn.com/compiler-playground/cffGkCrIwZvU/?ref=app
20th Feb 2024, 1:34 PM
Rain
Rain - avatar
+ 1
Wong Hei Ming , Yeah, but I'm talking about the substitution to his original code that Solo mentioned and I had already tried without success. Original code: n=input("enter file : ").lower() match n: case n.endswith(".gif"): print("image/gif") case n.endswith(".jpg"): print("jpeg/image") case n.endswith(".jpeg"): print("jpeg/image") case n.endswith(".ping"): print("jpeg/image") case n.endwith(".pdf"): print("pdf/text") case n.endswith(".txt"): print("txt/text") case n.endswith(".zip"): print("zip/text") case _: print("applicaton/octet-stream") That gets a TypeError, which one would think is because it tries to match str to bool. So you change it from, match n: to, match True: in order to match bool to bool. Like so. n=input("enter file : ").lower() match True: case n.endswith(".gif"): print("image/gif") case n.endswith(".jpg"): print("jpeg/image") [etc.] but it still gets TypeError. What causes that TypeError?
20th Feb 2024, 4:34 PM
Rain
Rain - avatar
0
I tried it on online visual studio code it gives me type errorSolo
19th Feb 2024, 8:01 AM
Yusof
Yusof - avatar
0
Yusof, try match True: ...😎
19th Feb 2024, 9:48 AM
Solo
Solo - avatar
0
Solo , Did you get match True: to work? I thought of it too, but it still produced the same error for me. TypeError: called match pattern must be a type
20th Feb 2024, 12:58 AM
Rain
Rain - avatar
0
Rain, no, this is just my guess, (I had neither the opportunity nor the time to check it). Based on logic "match n case n.endswith(".gif"):" – this is similar to the "if 'file.gif' == True:", which in principle is not true to compare, whereas "if True == True:" could fix the situation, since the method .endswith() returns a Boolean value. Personally, I would use a dictionary here instead of a match...case, it's much simpler and more ergonomic in my opinion.
20th Feb 2024, 6:42 AM
Solo
Solo - avatar
0
Solo , Yeah that's what I was trying too: since all the cases evaluate to bool, why not make the match bool? But the problem is apparently deeper and weirder.
20th Feb 2024, 7:05 AM
Rain
Rain - avatar
0
Just tried with IDLE. In the below code the first case runs, not the second. n=input("enter file : ").lower() match n: case ".gif": print("image/gif") case n.endswith(".jpg"): print("jpeg/image") # It runs if the input of n is ".gif", but not "somevalue.jpg"
20th Feb 2024, 10:22 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
Rain It should be match n: not match True: n = True match n: case True: print(True) case False: print(False) case _: print("Something else") It prints True
20th Feb 2024, 2:31 PM
Wong Hei Ming
Wong Hei Ming - avatar
0
You could
20th Feb 2024, 5:35 PM
luke strange
luke strange - avatar
0
Add more like false and true code
20th Feb 2024, 5:37 PM
luke strange
luke strange - avatar
0
Rain I take a little time to look for the documentation and found this. https://docs.python.org/3/reference/compound_stmts.html#match Under 8.6.4.10, it says: If name_or_attr is not an instance of the builtin type , raise TypeError. My guess (probably wrong), match True: where the "True" in the statement is not an instance.
21st Feb 2024, 1:37 AM
Wong Hei Ming
Wong Hei Ming - avatar