0

please guys i am really stuck, first of i have a question as to how to debug python code. like the debug50. and my second is with my code for vigenere.

import cs50
import sys

def main():
    # make sure the user gives the right parameters
    if len(sys.argv) != 2:
        print("python caesar.py key")
        exit(1)

    # make sure the input are valid alabetical characters
    if sys.argv[1].isalpha() == False:
        print("plase provide valid input")
        exit(2)

    # prompt the user for text to be ciphered
    print("plaintext: ", end="")
    plaintext = cs50.get_string()
    print("ciphertext: ", end="")

    # prepare to do the ciphering
    key_index = 0
    key_len = len(sys.argv[1])
    ciphertext = []

    # loop and cipher
    for characters in plaintext:
        if characters.isalpha():
            key_index = key_index % key_len
            ciphertext.append(cipher_this(characters, key_index))
        else:
            ciphertext.append(characters)
    # print the final solution
    print("".join(ciphertext))

def cipher_this(char, key):
    if char.isupper():
        return chr(((ord(char) - 65 + key - 65) % 26) + 65)
        key+=key
    elif char.islower():
        return chr(((ord(char) - 97 + key - 97) % 26) + 97)
        key+=key


if __name__=="__main__":
    main()

i cannot figure out what is going on with my code. please any help would be great. specifically how do i get at the i'th index of the key with python. i feel like C, i know i sound crazy saying this, is wayyy easier when it comes to getting at the index of the key, but i am so confused about this one. thanks for the help in advanced

0

If you know you are going to need the variable i inside the loop, instead of the for loop you have

for character in plaintext:
    # do stuff

use the following, which is closer to what you have been using in C

for i in range(10):
    # do stuff with plaintext[i]

I hope this helps! Happy coding!

Edit:

Also, when you call cipher_this(char, key), key is passed by value, so when you later change the value of key inside the function, this value is not reflected to key outside the function.

Also in cipher_this(char, key) you assume that if the current character in plaintext is lowercase/uppercase, then the character at the same index in keyword will be lowercase/uppercase too.

Also you never use the str that is passed as keyword from the user in the command line. You just use its length to wrap around key_index.

2
  • thank you for your response, so if how would i go about fixing the part where i assume that the key word is lower/uppercase, do i just stick to making it lowercase – trillkillahkam Jul 26 '17 at 3:09
  • still really lost\n pastebin.com/DHEbSYWV how would i go about using the range function in the for loop for this problem – trillkillahkam Jul 26 '17 at 3:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .