1 / 9

Five-minute starter task

Five-minute starter task. Write a function that takes a string argument and returns a spaced out version of the string E.g. spaceOut ("Hello") H e l l o. The spaceOut task. You can iterate over a string, like you can a list: for x in myString : do something

benita
Download Presentation

Five-minute starter task

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Five-minute starter task • Write a function that takes a string argument and returns a spaced out version of the string • E.g. spaceOut("Hello") H e l l o

  2. The spaceOut task • You can iterate over a string, like you can a list: • for x in myString: • do something • You cannot change the letters of a string one by one like this: • for x in myString • x = x + ' ' • You have to build up a NEW STRING and then RETURN IT • newStr = "" • for x in myString • newStr = ….

  3. Examples of string functions in Python Find full documentation here: http://docs.python.org/3/library/string.html

  4. Caesar Ciphers

  5. Examples • Plain text:The quick brown fox jumps over the lazy dog • Cipher text:uifrvjdlcspxogpykvnqtpwfsuifmbazeph

  6. Clues: You may find these lines of code useful (they are not in order!) • for x in plainText: • # do something with each letter • y = string.ascii_lowercase.find(x) + 1 • cipherText = cipherText + string.ascii_lowercase[y] • plainText = plainText.lower() • You may also find the % operator useful…

  7. More clues for every character in plain text: index = number of the character in the alphabet index = index + 1 cipher character = index'th letter of alphabet add cipher character to cipher text This will work in most cases, but you will have a problem when you come across a 'z' and when you come across an upper case letter, and when you come across a space or some punctuation character.

  8. Extension tasks • My example only returns cipher text in lower case. Can you write a function that returns mixed cases? • My example will raise an error if the plain text contains punctuation. Can you make your function cope with it? • Write a function that will encrypt txt using ANY rotation. • Write a function that will decrypt anything encrypted using a Caesar cipher.

  9. Transposition Ciphers

More Related