this was very helpful. i could not think thru the logic to [index: ] to make sure everything after the first digit was also a a digit. I was slicing and dicing and failing all over the place. very nice.
@NickRoeder Жыл бұрын
Freaking brilliant. I had 45 lines of code that passed check50 but not technically the requirements of the pset. I had to see how it was properly done. This is great. Thank you.
@deljimaetalk7766 Жыл бұрын
Mine is 59 lines 🤣 Got 8 /10. Brilliant is an understatement
@Pr0feBboy Жыл бұрын
I spent a whole day trying to find a way to check if there are middle numbers and finally decided to check a tutorial that was actually wrong.... Is it just me or this for a beginner with 1 month of experience in python is really difficult ? It's kind of discouraging to fail all the time
@6hbaufhehdj Жыл бұрын
Dont be discouraged. Be happy you are learning, this was a hard problem for me as I am a computer science student, but it comes down to learning the string methods.
@Pr0feBboy Жыл бұрын
@@6hbaufhehdj Thanks. Im still going but where I'll get 5 months from now no idea
@miquelr23539 ай бұрын
Exactly the same. Im trying now for about hour and a half, I wrote that last loop to check like 8 or 9 different ways. Each time it decided a different input to mess up This is very hard
@noisi_tc30688 ай бұрын
It is a difficult and steep learning curve and this problem is definitief not easy
@abdullahadel11416 ай бұрын
Thanks Omar for the amazing explanation, It really helped me a lot
@TheCodingCreator6 ай бұрын
Glad it helped!
@studywithshin22054 ай бұрын
if we set int(char) != 0 wouldn't 'CS50' return False? Why does it return True?? Can someone please explain ?
@MohammedArab-f6i4 ай бұрын
cus only the first number in the phrase will enter the if statments. If the first number is 0 then return false vai the else statment.
@getstart98 Жыл бұрын
wow, amazing!! my solutions passed the test but one was about 40 lines long and the other was almost 50😶
@minibus1351 Жыл бұрын
This worked for me def main(): plate = input("Plate: ") if is_valid (plate): print("Valid") else: print("Invalid") def is_valid(s): if 6 >= len(s) and s[0:2].isalpha() and s.isalnum(): for char in s: if char.isdigit(): index = s.index(char) if s[index:].isdigit() and int(char) != 0: return True else: return False return True if __name__ == "__main__": main()
@tchavofrund190811 ай бұрын
Hi thanks a lot for the explaination it is very concise ! However, I don't understand the role of the very last 'return True'. From what I understand, if all the 'if' statments are True, it goes all the way down to the last one and if the last one is True then it works. If not, there is the 'else' that returns False and then it is invalid. So why after that 'else' do we still need the 'return True' ? Wouldn't it mean that even if the 'if' above return False, at the end, it overwrites it and it is still True ? Thanks
@ravishankar-ix7qj10 ай бұрын
We used the last "Return True " if the string contains only letters. if there are no number it will not enter if condition....
@aigerimabseit8162 жыл бұрын
def main(): plate = input("Plate: ") if not plate.isalnum() or (len(plate) < 2 or len(plate) > 6): print("Invalid") return False if is_valid(plate): print("Valid") else: print("Invalid") def is_valid(s): if s.isalpha() or s[:2].isalpha() and s[-2:].isnumeric() and s[-2:][0] != "0": return True else: return False main() try this way too
@TheCodingCreator2 жыл бұрын
great work. but also try putting your code inside the is_valid() function, and leave the main function for calling the function and printing a message to the user.
@aristhera2 жыл бұрын
doesn't work. Let s = 'aaa888' That's valid, right? Now let it be 'aaa088' That's invalid because of the leading 0 but s[-2:][0] matches only 88 (and then 8) and is therefor True
@aristhera2 жыл бұрын
so you will definitely need a for-loop like in the video, check for isdigit(), create an index (see vid) and then s[index:][0] instead of [-2:] and check if the string before index is alphabetical with s[index - 1].isalpha()
@jessicaly88932 жыл бұрын
can you explain why there is a return True on line 18?
@TheCodingCreator2 жыл бұрын
because the for loop checks if a string containing digits is valid or not and that's it, so we also have to consider a string with no digits, and that's also a valid plate and the function should return true.
@mohammadamintorabi84152 жыл бұрын
Thanks. small improvement though, instead of " for char in s: if s.isdigit(): index = s.index(char) ..... you can say: for i in range(len(s)): if s[i:].isdigit() and s[i]!= '0': ..... "i" is your index
@BaraBarabere. Жыл бұрын
not working
@MohammedArab-f6i4 ай бұрын
yea but then you would have to change every instance of char to s[i], which honestly is more annoying then just assigning the index to a var 🦐
@KurtGibson-p4q6 ай бұрын
Can someone explain to me the syntax of line 14 i dont get why the : in [index:] ?
@seifyounes53065 ай бұрын
+ 1
@studywithshin22054 ай бұрын
please watch tutorials on index function. here in this code index is a variable assigned to index = s.index(char) if char is a digit and [index:] means starting from that index in the iterated string of variable char, it will be returned True. if there is an alphabet after that index, it will return False since numbers cannot be in the middle.
@btobmelody32172 жыл бұрын
can i do the for loop outside the if statement in line 10? like after that conditional, return true else false. then do the for loop?
@KawsarAhmed-xd4jg Жыл бұрын
Hey! So a bug may arise because the if statement at first handles most of the logic except it does not encounter the '0' or zero that comes just after the leading alphabets. In that case, it may return True which isn't the correct output. For this reason, it's necessary to implement the for loop inside the if statement to check whether the zero is positioned just after the alphabets or not. Hope you will find it helpful.
@chikamatthew9592 жыл бұрын
I don’t understand the s[0:2].isalpha() function in the program
@HollowSunny692 жыл бұрын
Check the hints in the problem set. It explains it well.
@TheCodingCreator2 жыл бұрын
it checks if the first two charachters in our string is alphabetic letters, because as the problem stated “All vanity plates must start with at least two letters.”
@jermovankatwijk22902 жыл бұрын
Makes sure that the first spots are always metters
@MrKamoliddin Жыл бұрын
the code should fail with inputs with , such as CS50P2.
@MrKamoliddin Жыл бұрын
but this works: def is_valid(s): if 2
@miquelr23539 ай бұрын
I like how i did check50 and instantly got 4 red ansers with this code
@mariakeramida1032 Жыл бұрын
Hi all. I m trying to solve the problem but the elif statement is not working. I checked everything but the CS50P has a result Valid. Can you please help me with my mistake? Here is my code: def main(): plate = input("Plate: ") if is_valid(plate): print("Valid") else: print("Invalid") def is_valid(s): if len(s) >= 2 and len(s)
@hongcao3092 жыл бұрын
appreciate this video
@jdubiez2 жыл бұрын
Why do you is .isdigit instead of .isnumeric ? Or does it not matter?
@hernansaa Жыл бұрын
both work well in this case.
@lucascamarasa20812 жыл бұрын
Do you mind explaining line 13 and line 14 of your final code again? I don't quite get the syntax, it looks cryptic to me
@abdullashafi5802 жыл бұрын
yup this line was confusing! but there is a built in function in python called index() "The Python index() method helps you find the index position of an element or an item in a string of characters or a list of items. It spits out the lowest possible index of the specified element in the list. In case the specified item does not exist in the list, a ValueError is returned " __google
@floki0407 Жыл бұрын
I did same for first three.. But instead i used nested loop for each and ended up in problem.. For next 2 conditions... But writing them in only one line and nesting only tei conditions make it better......
@nted2 жыл бұрын
Loops are annoying to me because I break them very easily
@TheCodingCreator2 жыл бұрын
sometimes it be annoying, but you have to get used to it because it is very useful and most of the time you will need loops in your program.
@9ionet Жыл бұрын
For those who don't quite get it, hopefully below code is less cryptic. def main(): plate = input("Plate: ") if is_valid(plate): print("Valid") else: print("Invalid") def is_valid(s): if not 2
@julialett9 ай бұрын
hii can you please explain to me the "stack = []" part of the code pls?
@seifyounes53065 ай бұрын
i still can't get it or understand it at all
@seifyounes53065 ай бұрын
i feel like im the only one who can't do it
@cruseder2 Жыл бұрын
def main(): plate = input("Plate: ") if is_valid(plate): print("Valid") else: print("Invalid") def is_valid(s): list = [] length = len(s) - 1 if len(s) < 2 or len(s) > 6: return False for i in range(length): if s[i].isnumeric() and s[i+1].isalpha(): return False for i in s: if i.isnumeric(): list.append(i) if i[0] == 0: return False if s.isalnum() and s[0:2].isalpha(): return True if __name__ == "__main__": main()
@tonybuss82422 жыл бұрын
Your code needs an improvement, the way you typed it above will not work.... after your if 6 >= len(s) >= 2 and s[0:2].isalpha() and s.isalnum(): you need to add if s.isalpha(): return True if you do not, a standar all alfa AAAAAA will not work. // TAB
@TheCodingCreator2 жыл бұрын
i tried and it is working, why do you think it won't work?
@jermovankatwijk22902 жыл бұрын
Jea indeed, without this addition NVROUS would be Invalid
@NickRoeder Жыл бұрын
AAAAAA.isalnum is True so it will return valid. It does indeed return valid too.
@janardhan2jordan2 жыл бұрын
thanks a lot bud
@simeonrf7047 Жыл бұрын
def main(): plate = input("Plate: ") if is_valid(plate): print("Valid") else: print("Invalid") def is_valid(input_string): while True: length = len(input_string) if ( input_string[0:2].isalpha() and 2
@ramireddyvarun26 Жыл бұрын
feel so foolish! I tried enumerate to get the index etc but never explored index directly from string methods! lesson learnt ... read the bloody documentation! :) Thanks though.
@amirprx310 ай бұрын
❤❤
@curiousLeafy Жыл бұрын
``` # import re def main(): plate = input("Plate: ") if is_valid(plate): print("Valid") else: print("Invalid") def is_valid(s): if(len(s) < 2): return False s = s.lower() length = len(s) is_min_two_letters = s[0: 2].isalpha() valid_length = length >=2 and length
@adarshgurung94322 жыл бұрын
Please atleast try of explain what you are doing.
@TheCodingCreator2 жыл бұрын
which part do u need explanation for?
@adarshgurung94322 жыл бұрын
@@TheCodingCreator I dont understand the use of "char" , "index" and line 14 .
@TheCodingCreator2 жыл бұрын
@@adarshgurung9432 my goal was to look for the first digit that appears in a string, so that is why I made the for loop. after knowing that the character is a digit, I want to check if that character doesn't equal 0, and each subsequent character is also a digit (line 14). char, and index are just variable names, you can call them whatever u like. for further understanding, let's assume that our string is "CS50" and we are on line 11. first loop, char will store "C", then we check if char is a digit on line 12, since it is not a digit, our loop ends. second loop, char will store "S", then again we check if char is a digit, since it is not, our loop ends. third loop, char will store "5", then we check if char is a digit, since it is a digit, the variable index will store the index of this character which will be 2 (index count start from 0), then on line 14, s[index:] slices our string starting at index till the last character, in our case: s[2:], which returns "50", then we check if that string contains only digits (that is true). in line 14 we also check if char != 0, in our case 5 != 0, which return true. i hope that makes sense.
@adarshgurung94322 жыл бұрын
@@TheCodingCreator Thanks, I understand most of it except, Which line of code ensures that if first numeric value is not zero. I guess it is int(char) != 0? But I don't understand how.
@adarshgurung94322 жыл бұрын
I mean in line 14 ( int(char) != 0), won't this line disturb the "0" in "CS50" as input, even if 0 is at the end of the word CS50?
@ayounis5158 Жыл бұрын
what do you think of my solution: def is_valid(inp): if 2
@koenokatachiii9218 Жыл бұрын
trash
@ayounis5158 Жыл бұрын
@@koenokatachiii9218 fatherless behaviour
@koenokatachiii9218 Жыл бұрын
@@ayounis5158 youre editing anime girls, get a life
@ayounis5158 Жыл бұрын
@@koenokatachiii9218 get a family, oh you can't, srry :(
@koenokatachiii9218 Жыл бұрын
@@ayounis5158 what an insult!!!!!!!!!!!!!
@TheFailMistress2 жыл бұрын
i know an egyptian when i hear one 😅
@TheCodingCreator2 жыл бұрын
was it that obvious 😂
@christopherreina4901 Жыл бұрын
without using index: def main(): plate = input("Plate: ") if is_valid(plate): print("Valid") else: print("Invalid") def is_valid(s): if s[0:2].isalpha() and 2
@oltionr2 жыл бұрын
That's not fair to distribute the solutions. I hope CS50 will not consider this type of solution. Anyone should try to learn it on their own.
@TheCodingCreator2 жыл бұрын
that is not the solution, that is just a solution. the point of these videos is not to make you copy and paste the solution and submit it, the point is to explain the language and learn how to solve problems and make programs with it. but yeah everyone should try it on their own, I should probably mention that in my videos.