python局部变量调用_分配前可能会引用局部变量-Python

I've been trying to make an encryption and decryption system but I have run into a small error. Here is my code:

import sys

import pyperclip

def copy(data):

question = input("Copy to clipboard? ")

if question.lower() == 'yes' or question.lower() == 'y':

pyperclip.copy(data)

print("Encrypted message copied to clipboard.")

rerun()

elif question.lower() == 'no' or question.lower() == 'n':

rerun()

else:

print("You did not enter a valid input.")

copy(data)

def rerun():

ask = input("\nWould you like to run this program again? ")

if ask.lower() == "yes" or ask.lower() == "y":

print(" ")

run()

elif ask.lower() == 'no' or ask.lower() == 'n':

sys.exit("\nThank you!")

else:

print("You did not enter a valid input.")

rerun()

def encrypt(key, msg):

encrypted_message = []

for i, c in enumerate(msg):

key_c = ord(key[i % len(key)])

msg_c = ord(c)

encrypted_message.append(chr((msg_c + key_c) % 127))

return ''.join(encrypted_message)

def decrypt(key, encrypted):

msg = []

for i, c in enumerate(encrypted):

key_c = ord(key[i % len(key)])

enc_c = ord(c)

msg.append(chr((enc_c - key_c) % 127))

return ''.join(msg)

def run():

function_type = input("Would you like to encrypt or decrypt a message? ")

if function_type.lower() == "encrypt" or function_type.lower() == "e":

key = input("\nKey: ")

msg = input("Message: ")

data = encrypt(key, msg)

enc_message = "\nYour encrypted message is: " + data

print(enc_message)

copy(data)

elif function_type.lower() == "decrypt" or function_type.lower() == "d":

key = input("\nKey: ")

question = input("Paste encrypted message from clipboard? ")

if question.lower() == 'yes' or question.lower() == 'y':

encrypted = pyperclip.paste()

print("Message: " + encrypted)

elif question.lower() == 'no' or question.lower() == 'n':

encrypted = input("Message: ")

else:

print("You did not enter a valid input.")

run()

decrypted = decrypt(key, encrypted)

decrypted_message = "\nYour decrypted message is: " + decrypted

print(decrypted_message)

copy(decrypted)

else:

print("\nYou did not enter a valid input.\n")

run()

run()

It says local variable 'encrypted' might be referenced before assignment and highlights

decrypted = decrypt(key, encrypted)

under the run() function.

Is it because I used the variable 'encrypted' in other functions? If so, how would I fix this and still maintain the functionality of my program?

I am relatively new to python so I would appreciate it if you could explain your answers.

解决方案local variable 'encrypted' might be referenced before assignment

is a warning generated by the linter.

This is because the linter sees that encrypted is assigned values inside two if conditions

if question.lower() == 'yes' or question.lower() == 'y':

and

elif question.lower() == 'no' or question.lower() == 'n':

however, the linter cannot know that these two if conditions are complementary to each other. So, considering the case when none of the conditions is true, the variable encrypted will end up uninitialized.

To get rid of this warning, you can simply initialize the variable before any of the if conditions with None value

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值