0

I have written the following code for vigenere.py, but I do not know how to incorporate into my code the idea that after the for loop reaches the final character of the command-line argument, it automatically restarts from the first character of the command-line argument. My current program works only when the plain text and the command-line argument contain exactly the same number of characters, but it does not work when, for instance, the plain text contains more characters than the command-line argument. Any help will be greatly appreciated, thank you!

import cs50
import sys

def main():
    if len(sys.argv) != 2:
        print("You are wrong: restricted to only one command-line argument!")
        return False

    if not (sys.argv[1]).isalpha():
        print("You are wrong: accept only alphabets!")
        return False

    plain_text = cs50.get_string()
    i = 0
    for j in plain_text:
        if plain_text.isalpha():
            k = sys.argv[1][i]
            if (sys.argv[1]).isupper() & plain_text.isupper():
                print("{}".format(chr((((ord(j) - 64) + (ord(k) - 64)) % 26) + 64)), end="")
            elif (sys.argv[1]).islower() & plain_text.isupper():
                print("{}".format(chr((((ord(j) - 64) + (ord(k) - 96)) % 26) + 64)), end="")
            elif (sys.argv[1]).isupper() & plain_text.islower():
                print("{}".format(chr((((ord(j) - 96) + (ord(k) - 64)) % 26) + 96)), end="")
            elif (sys.argv[1]).islower() & plain_text.islower():
                print("{}".format(chr((((ord(j) - 96) + (ord(k) - 96)) % 26) + 96)), end="")
            i += 1
        else:
            print("{}".format(j), end="")
    print(" ")

if __name__ == "__main__":
    main()

1 Answer 1

0

Inside for j in plain_text: loop insert an if condition that assigns i=0 whenever i becomes len(sys.arg[1]). That way, k wraps around and starts from the first character when k[i] reaches the end of the string.

3
  • Thank you for your advice. I added the following lines if i == len(sys.argv[1]): i = 0 right under i += 1. Regardless of the lengths of the command-line argument or plain_text, the program now works when both the command-line argument and plain_text contain only small letters. When either contains a capital letter, the program outputs only a white space. Please advise as to fix it, thanks a lot @vaish !!
    – Alex Chan
    Commented Mar 8, 2017 at 7:00
  • Oh, change your 64s to 65 and 96s to 97. Just look at the ascii table once and you'll figure it out. Next: The main problem in your code is, the for loop. You are iterating through each char of your plaintext. So j becomes your plaintext character and k is your key's character. So instead of doing if (sys.argv[1]).isupper() & plain_text.isupper(): you should be doing if k.isupper() and j.isupper(): because k and j are the characters u wanna work on. sys.argv[1] and plain_text are whole strings. After trying those stuff, ur code should work. If it works don't forget to upvote dude, lol.
    – Vaish MK
    Commented Mar 8, 2017 at 7:43
  • I had to make one more change other than those you already suggested to make it work, but that additional change is also about the issues you raised. THANK YOU VERY MUCH!!!!!!! @vaish
    – Alex Chan
    Commented Mar 9, 2017 at 8:08

You must log in to answer this question.

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