Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to process a String input including single quotation
    primarykey
    data
    text
    <p>I wrote a script by python which prompts users to Enter a string, then encode or decode it. However, one of the users Entered single quotation( this -> ' ) so the script stopped. How can I solve this problem. I would like to accept single quotation as an input. the script looks like this:</p> <pre><code>while True: input = raw_input('Enter a word or sentence') if input == 'done':break n = raw_input('Enter an encode number') print encode(input,n) #this function encodes the input by n and returns encoded words </code></pre> <p>There are ord and chr function in the encode function.</p> <p>this is encode function</p> <pre><code>def encode(word,n): """ word: strings you wanna encode or decode n: a number you wanna encode or decode by """ code = 0 rotate = '' for letter in word: if ord(letter) == ord(',') or ord(letter)== ord('.') or ord(letter) == ord(' ') or ord(letter) == ord('?') or ord(letter) == ("'"): rotate += letter continue rotate += rotate_num(letter,n,letter.islower()) return rotate def rotate_num(letter,n,lower): """ n: a number you wanna encode or decode by lower:Assign True if the letter is lowercase, otherwiser False """ if lower: a = 'a' z = 'z' else: a = 'A' z = 'Z' code = ord(letter)+n if code &gt; ord(z): code = code - 1 - ord(z)+ ord(a) if code &lt; ord(a): code = ord(z) - ( ord(a)-code-1) return chr(code) </code></pre> <p>(the rotate_num function should be indented. I missed copying)</p> <p>the problem was that the input was too long(He entered too long sentence). However, other problem remained: in the encode function, I wrote: if the letter is a single quotation, it should not be encoded, but actually, the single quotation was encoded</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload