5
cipher
8y

Hi so I'm learning python in my spare time and I'm in a national competition. I've been told that programming is something my college has always lacked in and in the competition they fortunately use python throughout the problems. I have some example problems used in the last year competition (it was publicly released) and I'm going through them to get an idea of the problems we/I will face. Now I'm still learning python but I understand some of the code at hand. However I still need a little bit of help to understand some of it which will also help me get to a resolution.
Some of the questions I have are:
1. What exactly is the ordinal? I've done some research and I have a small idea but I couldn't find anything to really fill me in and explain how to use it, well in python at least. I saw an example for Pascal but that didn't do much.
2. What is the sys.argv? "The list if command line arguments passed to a python script". I'm not quite understanding that.
3. I know for is used for looping and I know an example say "for a in range(10):" but I'm not understanding the for c in password:
4. Where does the 1000 come from in the builder += 1000.
5. What does the 83 represent after ord(password[1])
6. I know the if statement is saying if this then do this so if __name__ == "__main__":
main()
It's saying call in the function main but where does the name and main come in that part?
Here is the image:

Thank you for your responses in advanced!
One person doesn't have to answer all. Time is precious I understand.

Comments
  • 4
    A lot of good questions which would mostly be answered in a beginning python course such as the one on Udacity which is recommend you check. But to answer some for now:
    1- The ordinal of a character gives it a value that can be compared to another so that "a" < "b" as ord('a') == 97 and ord('b') == 98. It might help you to just rest out one liners of what functions do to different inputs to help get a feel for them.
  • 3
    2- if you this:
    python script.py item1 item2
    sys.argv[1] will be "item1" and sys.argv[2] will be "item2" etc. Again, setting up a tiny script which does only this and playing with different inputs should help you get a feel for it.
    3- in this case, password is a variable which was initialised with the first item as described above and so the for loop steps through each character of the string because a string can be seen as an array of characters. This is usually a 'for each' loop in other languages.
  • 3
    4- Firstly note that it is builder==1000 which is checking that the sum of the values for each character in the string password equal to 1000 and the rest of the line ensures that the password string is exactly 10 characters long while...
    5- The third check is to ensure the second character is 'S' which you can find by using chr(83) which converts a Unicode value back to a character. So reversing ord() here. The online python docs are also of course great sources of info on these questions.
    6- This helps you when linking scripts together... Of a script is run directly, then __name__ will be equal to "__main__" but if you import it from another it will not. This let's you give a script functionality is it is run directly which does not run is you import it to another script.
    Hope that helps as a start but keep playing with it... If the docs don't help you enough, trial and error on commands you're uncertain of should get you there.
  • 1
    2. When you pass arguments to a script then those arguments will be stored in sys.argv array. The first element of the array is the script to which you pass then the next elements will be the arguments which you are passing example

    python main.py random

    sys.argv will be ['main.py', 'random']
  • 0
    @L33tCh Thank you for taking the time to answer my questions. I appreciate it!
    Are you referring to the free course programming foundations with python? I've had it bookmarked on my phone for a while but completely forgot about it until now. I'll make sure to start when I can.
    Now that I have a better understanding I'll go through and do trial and error and mess around with one liners as you've said, thank you again.
    Also my apologies, I meant builder == 1000.
  • 0
    @Anwesh43
    Thank you for your response and the example provided.
  • 1
    @QuantumAtom No problem. And yes, that's the course. I've been programming quite a while but only started python a few months ago so scanned through that course and a few others... I like to get a view of the differences and recommended/common code styles when picking up a new language. And personally, I learn a lot more in practice than study so these courses and just playing around are great for that.
  • 0
    @L33tCh
    That's awesome. I haven't been for so long, only this fall semester but I'm liking it. This won't be my career but I'll do it on the side as a hobby and it'll maybe be a fallback incase my plan A doesn't work out.
    But I 100% agree. Especially in this type of area hands on is very important.
Add Comment