Question

In: Computer Science

Please fix this python script, I keep getting a return outside function error and cannot get...

Please fix this python script, I keep getting a return outside function error and cannot get it to run:

def caesar(plainText, shift):
    cipherText = ""
for char in plainText:
    newChar = ord(char) + shift
    if newChar > 128:
      newChar = newChar % 128
    finalChar = chr(newChar)
    cipherText += finalChar
return cipherText
text = input("Enter a message: ");
s = input("Enter the distance value: ");
print (caesar(text, int(s)));

Solutions

Expert Solution

The problem with your code was bad indentation. The spacing was incorrect. Fixed it. Please find the updated code below.







def caesar(plainText, shift):
    cipherText = ""
    for char in plainText:
        newChar = ord(char) + shift
        if newChar > 128:
          newChar = newChar % 128
        finalChar = chr(newChar)
        cipherText += finalChar
    return cipherText



text = input("Enter a message: ");
s = input("Enter the distance value: ");
print (caesar(text, int(s)));
#end of code





























But the above method lacks correctness. A better and correct method of implementing Caesar cipher would be as follows. Use this method if you want, else ignore it









def caesar(plainText, shift):
    cipherText=""
    #looping through each character
    for char in plainText:
        #checking if char is upper case
        if char.isupper():
            #checking if shifting will go beyond 'Z'
            if (ord(char)+shift) > ord('Z'):
                #finding how many spaces it will be moved away from 'Z'
                index=ord(char)+shift-ord('Z')-1
                #adding that distance to 'A' to get wrapped around, adding this
                #value to cipherText
                cipherText+=chr(ord('A')+index)
            else:
                #shifting normally
                cipherText+=chr(ord(char)+shift)
        elif char.islower():
            #doing the same for lower case
            if (ord(char)+shift) > ord('z'):
                index=ord(char)+shift-ord('z')-1
                cipherText+=chr(ord('a')+index)
            else:
                cipherText+=chr(ord(char)+shift)
        else:
            #other characters are not encrypted, simply appending to cipherText
            cipherText+=char
    #returning
    return cipherText

Related Solutions

I keep getting an error that I cannot figure out with the below VS2019 windows forms...
I keep getting an error that I cannot figure out with the below VS2019 windows forms .net framework windows forms error CS0029 C# Cannot implicitly convert type 'bool' to 'string' It appears to be this that is causing the issue string id; while (id = sr.ReadLine() != null) using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; namespace Dropbox13 { public partial class SearchForm : Form { private List allStudent = new List(); public SearchForm() { InitializeComponent(); } private void SearchForm_Load(object...
Keep getting error where the code cannot read the text file and create an arraylist of...
Keep getting error where the code cannot read the text file and create an arraylist of objects from it. HouseListTester: import java.util.*; //Hard codes the criteria public class HouseListTester { static HouseList availableHouses; public static void main(String []args) { availableHouses = new HouseList("C:\\Users\\jvs34\\Downloads\\houses.txt"); Criteria c1 = new Criteria(1000, 500000, 100, 5000, 0, 10); Criteria c2 = new Criteria(1000, 100000, 500, 1200, 0, 3); Criteria c3 = new Criteria(100000, 200000, 1000, 2000, 2, 3); Criteria c4 = new Criteria(200000, 300000, 1500,...
Why am I getting this error using 000webhost , but I do not get this error...
Why am I getting this error using 000webhost , but I do not get this error running XAMPP? Warning: Cannot modify header information - headers already sent by (output started at /storage/ssd2/006/14881006/public_html/test/index.php:1) in /storage/ssd2/006/14881006/public_html/test/index.php on line 8 Code is below. ******************************************************************************************************************** <!DOCTYPE html> <?php    //Determine if the submit button has been clicked to set the cookie name and value    if (isset($_POST['name'])){            $cookie_name = $_POST['name'];            $cookie_value = $_POST['value'];            setcookie($cookie_name, $cookie_value,...
I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
I'm Getting an "unindented error" Please fix the bolded codes. Because I don't know whats going...
I'm Getting an "unindented error" Please fix the bolded codes. Because I don't know whats going on. Thank You. # This program exercises lists. # The following files must be in the same folder: # abstractcollection.py # abstractlist.py # arraylist.py # arrays.py # linkedlist.py # node.py # input.txt - the input text file. # Input: input.txt # This file must be in the same folder. # To keep things simple: # This file contains no punctuation. # This file contains...
Syntax error in C. I am not familiar with C at all and I keep getting...
Syntax error in C. I am not familiar with C at all and I keep getting this one error "c error expected identifier or '(' before } token" Please show me where I made the error. The error is said to be on the very last line, so the very last bracket #include #include #include #include   int main(int argc, char*_argv[]) {     int input;     if (argc < 2)     {         input = promptUserInput();     }     else     {         input = (int)strtol(_argv[1],NULL, 10);     }     printResult(input);...
I keep on get a redefinition error and an undefined error. Customer List (1) CustomerList() and...
I keep on get a redefinition error and an undefined error. Customer List (1) CustomerList() and ~CustomerList() - default constructor and destructor. (2) bool addStore(Store*s) - Add an instance of store to the linked list. Return true if successful. (3) Store *removeStore(int ID) - Locate a Store in the list if it exists, remove and return it. (4) Store *getStore(int ID) - Locate a Store in the list and return a pointer to it. (5) bool updateStore(int ID, char *name,...
This is in Python I am getting an error when I run this program, also I...
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help! #Input Section def main(): name=input("Please enter the customer's name:") age=int(input("Enter age of the customer: ")) number_of_traffic_violations=int(input("Enter the number of traffic violations: ")) if age <=15 and age >= 105: print('Invalid Entry') if number_of_traffic_violations <0: print('Invalid Entry') #Poccessing Section def Insurance(): if age < 25 and number_of_tickets >= 4 and riskCode == 1: insurancePrice = 480 elif age >=...
I am getting an error at linen 57 and can't figure out how to fix it....
I am getting an error at linen 57 and can't figure out how to fix it. // Java program to read a CSV file and display the min, max, and average of numbers in it. import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; public class Main {     // method to determine and return the minimum number from the array     public static int minimum(int numbers[])     {         int minIdx = 0;         for(int i=1;i<numbers.length;i++)         {             if((minIdx...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1478) at Main.main(Main.java:34) Your output Welcome to the food festival! Would you like to place an order? Expected output This test case should produce no output in java import java.util.Scanner; public class Main {    public static void display(String menu[])    {        for(int i=0; i<menu.length; i++)        {            System.out.println (i + " - " + menu[i]);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT