我再次需要一些帮助。问题是,当我调用应该添加到变量的函数时,变量保持其原始值。
代码:
import random
def christncombat(x,y,z):
x += 1
if x > 26:
x = 1
y += 1
if y > 26:
y = 1
z += 1
if z > 26:
z = 1
x = 1
y = 1
encrypt = {65:90, 66:90, 67:72, 68:78, 69:77, 70:83, 71:87, 72:67, 73:73, 74:89, 75:84,
76:81, 77:69, 78:68, 79:79, 80:66, 81:76, 82:82, 83:70, 84:75, 85:85, 86:86,
87:71, 88:88, 89:74, 90:65}
encrypted = []
r1 = random.randint(1,26)
r2 = random.randint(1,26)
r3 = random.randint(1,26)
security = str(r1) + "a" + "-" + str(r2) + "b" + "-" + str(r3) + "c"
input_en = input("Zadejte text, ktery chcete zasifrovat (pouzivejte velka pismena): ")
for i in range(0,len(input_en)):
coded = input_en[i]
coded = ord(coded)
coded = encrypt[coded]
full = r1 + r2 + r3
coded += full
while(coded > 90):
rekt = coded - 90
coded = 65 + rekt
christncombat(r1,r2,r3)
full1 = r1 + r2 + r3
coded += full1
while(coded > 90):
rekt = coded - 90
coded = 65 + rekt
done = chr(coded)
encrypted.append(done)
print(encrypted)
print("Bezpecnostni kod je",security)
虽然问题可能出在这一部分:
def christncombat(x,y,z):
x += 1
if x > 26:
x = 1
y += 1
if y > 26:
y = 1
z += 1
if z > 26:
z = 1
x = 1
y = 1
答案 0 :(得分:1)
也许您想从函数中返回值?您可以将值放在列表中并返回列表:
def christncombat(x,y,z):
x += 1
if x > 26:
x = 1
y += 1
if y > 26:
y = 1
z += 1
if z > 26:
z = 1
x = 1
y = 1
value_list = [x, y, z]
return value_list
现在,您可以在代码的主要部分调用该函数:
new_value_list = christncombat(r1,r2,r3)
现在,在你的new_value_list中,你将r1,r2和r3全部改为你想要的那样。现在,您可以访问所有变量:
new_r1 = new_value_list[0]
new_r2 = new_value_list[1]
new_r3 = new_value_list[2]
答案 1 :(得分:1)
您需要从函数返回修改后的值,如下所示:
def christncombat(x,y,z):
x += 1
if x > 26:
x = 1
y += 1
if y > 26:
y = 1
z += 1
if z > 26:
z = 1
x = 1
y = 1
return x, y, z
然后在调用代码中,您可以捕获这些返回的值,如下所示:
r1, r2, r3 = christncombat(r1, r2, r3)