Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A line has the form name:mark, such as Asha:72. Write a function read_line(line) that gives back a tuple (name, mark) with the name tidy (the first letter big, the rest small) and the mark as a whole number. For a bad line (no colon, an empty name, or a mark that is not all digits), it gives back None. Test it on "asha:72", " RAVI : 85 ", "kabir", ":50" and "zoya:ten".
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. Cut at the first colon with partition, clean both parts, and check before you trust.
Check.
"asha:72" gives ("Asha", 72)." RAVI : 85 " is cleaned by strip on both parts, and gives ("Ravi", 85)."kabir" has no colon, so sep is empty: None.":50" has an empty name: None."zoya:ten" has a mark that is not all digits: None.Answer: ("Asha", 72), ("Ravi", 85), None, None, None.
Log in to join the discussion.
Loading…