我的elif语句总是出现无效语法错误

2024-05-23 22:38:59 发布

您现在位置:Python中文网/ 问答频道 /正文

之前我在每个选项选择中重复打印菜单上的类似代码,但我将其从while改为if和elif,但不起作用?它总是出现语法错误

option = 0

while option !=4:

    # Prints the menu to the screen
        print("*** Menu *** \n")
        print("1. Encrypt string")
        print("2. Decrypt string")
        print("3. Brute force decryption")
        print("4. Quit \n")

# Prompts the user
option = int(input("What would you like to do [1,2,3,4]? "))

# input error

if option ==1:
#if the user's selection equals 1


            # Prints to the screen the chosen option and prompts the user to input
            print("In command 1 - encrypt string \n")
            string = input("Please enter string to encrypt: ")
            offsetvalue = int(input("Please enter positive offset value: "))
            print("")

# input error
while offsetvalue > 26 or offsetvalue < 1:
    offsetvalue = int(input("Please enter a positive offset value between 1 - 26: "))

    # Assigns the 'Encryption' variable
    # letter which is encrypted

    Encryption = ""

# Loops the input in variable 'code' for each letter
for letter in string:
    # Converts each letter in 'code' to an ASCII value
    # and adds the positive offset value
    encryption_num = ord(letter)
    encryption_num += offsetvalue

# Loops the ASCII value
if encryption_num > 126:
        encryption_num -= 94
        # adds it to the total string
        encryption_num = chr(encryption_num)
        Encryption += encryption_num
        # encrypted string to the screen
        print("\nEncrypted string:")
        print(Encryption)

elif option == 2:

        # prompts the user to input
        print("In command 2 - decrypt string \n")
        string = input("Please enter string to decrypt: ")
        offsetvalue = int(input("Please enter negative offset value: "))
        print("")

# input error

while offsetvalue < -26 or offsetvalue > -1:
    offsetvalue = int(input("Please enter negative offset value between -1 - -26: "))

# Assigns the 'Decryption' variable
# letter which is decrypted
    Decryption = ""

# Loops the input in variable 'string'

for letter in string:
    # adds variable 'string' to an ASCII value
    # adds negative offset value
    decryption_num = ord(letter)
    decryption_num += offsetvalue
    # Loops the ASCII value to beginning if True
    if decryption_num < 32:
            decryption_num += 94
            # Converts the letter back into a string and adds it to the total string
            decryption_num = chr(decryption_num)
            Decryption += decryption_num
            # Prints the entire decrypted string to the screen
            print("\nDecrypted string:")
            print(Decryption)

这就是它突出显示elif语句错误的地方不确定原因?它在python中出现一条无效消息。

^{pr2}$

Tags: thetoinputstringifvaluenumencryption
1条回答
网友
1楼 · 发布于 2024-05-23 22:38:59

whilefor之后不能有elifelif只能在if或另一个{}之后。你的意思是把while缩进到前一个if之下吗?在

你的代码有其他问题。您似乎正在将字符串与数字(Decryption < len(letter))进行比较。在

你有这样的东西:

if option == 2:
    # stuff

while something:
    # stuff

elif option == 3:
    # stuff

你不能那样做。你可能是这个意思:

^{pr2}$

但是,我不能确切地告诉您要更改什么,因为我不确切地知道您希望代码执行什么操作。我建议您阅读the Python tutorial,以熟悉Python的基础知识。在

相关问题 更多 >