我的代码遇到问题,否则我不会在这里。我在函数中运行我的代码,并且当我重新读取我的代码时,它已经到了错误,看起来有0个错误。我怎样才能解决这个问题?代码都在我的程序中正确缩进。我的代码是:
import os
os.system('clear')
import random
import time
import math
def main_menu():
menu = input("Please select the number of your choice: 1)Encrypt, 2)Decrypt, 3)Exit or 4)Extended encrypt \n")
if(menu == '1' or menu == 'Encrypt' or menu == 'encrypt' or menu == 'ENCRYPT'):
return 1
elif(menu == '2' or menu == 'Decrypt' or menu == 'decrypt' or menu == 'DECRYPT'):
return 2
elif(menu == '3' or menu == 'Exit' or menu =='exit' or menu == 'EXIT'):
return 3
elif(menu == '4' or menu =='Extended Encrypt' or menu == 'extended encrypt' or menu == 'EXTENDED ENCRYPT' or menu == 'Extended' or menu == 'extended' or menu == 'EXTENDED'):
return 4
else:
return 5
def menu_encrypt():
print("Welcome to code encryption \n")
usertext = file_read()
offsetcode = offset_generator()
offsetcalc = offset_calc(offsetcode)
plrencrypt = encrypt_str(usertext, offsetcalc)
print(plrencrypt, "\n")
write_file(plrencrypt)
os.system('clear')
def offset_generator():
c = 0
offset = ""
while c < 8:
num = random.randint(33,126)
num = chr(num)
offset = offset + num
c = c + 1
print("This is your code for decryption \n")
print(offset, "\n")
print("Please remember this code as it is important for decryption \n")
return offset
def file_read():
while True:
try:
userfile = input("Please select the file you wish to use without the extension: ")
print("")
f = open(userfile + ".txt")
userprint = f.read()
return userprint
break
except IOError:
print("ERROR! The choice you have selected is invalid \n")
def offset_calc(offsetcode):
osgen = 0
for b in offsetcode:
b = ord(b)
osgen = osgen + b
osgen = math.floor(osgen/8)-32
return osgen
def encrypt_str(usertext, offsetcalc):
userencrypt = ""
for z in usertext:
z = ord(z)
if z == 32:
z = chr(z)
userencrypt = userencrypt + z
else:
z = z + offsetcalc
if z > 126:
z = z - 94
z = chr(z)
userencrypt = userencrypt + z
return userencrypt
def write_file(plrencrypt):
newname = input('Please choose the name of your file: ')
fiile = newname + (".txt")
newfile = open(fiile, "w")
newfile.write(plrencrypt)
newfile.close()
def input_offset(userarray):
usergen = 0
for a in userarray:
a = ord(a)
usergen = usergen + a
usergen = math.floor(usergen / 8) -32
return usergen
def menu_decrypt():
print("Welcome to code decryption \n")
userprint = file_read()
userarray = ""
while len(userarray) < 8:
charcode = input("Please enter each character of the 8 character code you were given during encryption: ")
userarray = userarray + charcode
usergen = input_offset(userarray)
userdecrypt = decrypt_str(userprint, usergen)
print(userdecrypt)
time.sleep(5)
os.system('clear')
def decrypt_str(userprint, usergen):
userdecrypt = ("")
for y in userprint:
y = ord(y)
if y == 32:
y = chr(y)
userdecrypt = userdecrypt + y
else:
y = y - usergen
if y < 33:
y = y + 94
y = chr(y)
userdecrypt = userdecrypt + y
return userdecrypt
def menu_extended():
print("Welcome to code encryption \n")
usertext = file_read()
offsetcode = offset_generator()
offsetcalc = offset_calc(offsetcode)
plrencrypt = extended_str(usertext, offsetcalc)
print(plrencrypt, "\n")
write_file(plrencrypt)
os.system('clear')
def extended_str(usertext, offsetcalc):
countering = 0
userencrypt = ""
for b in usertext:
b = ord(b)
if b == 32:
b = chr(b)
usertext = usertext - b
else:
while countering < 5:
b = b + offsetcalc
if b > 126:
b = b - 94
b = chr(b)
userencrypt = userencrypt + b
countering = countering + 1
if countering == 5:
countering = 0
userencrypt = userencrypt + ' '
return userencrypt
while True:
total = 0
menupoints = main_menu()
total = total + menupoints
if menupoints == 1:
menu_encrypt()
elif menupoints == 2:
menu_decrypt()
elif menupoints == 3:
print("")
print("Thank you for using our services", "\n")
os.system('clear')
break
elif menupoints == 4:
menu_extended()
elif menupoints == 5:
print("ERROR! The choice you have selected is invalid", "\n")
main_menu()
else:
()
答案 0 :(得分:0)
呃,当我将代码复制并粘贴到我的Python 3解释器中时,似乎可以解决这个问题。我使用数字来加密和解密几个文件以标记我的选择。
我唯一的诊断是你的机器中也可能有Python 2的副本,并且你的程序可能在Python 2下意外执行。你的整个程序都是有效的Python 2代码,但是在Python 2中input()的行为是不同的和古怪的。如果是这样的话,添加一个#! py -3到代码的顶部,如下所示:
#! py -3
import os
等
正如其他人所说的那样,如果你在提问时可以更具体一点,那就是A +。我只是假设'错误'你的意思是当用户输入无效选择时显示错误文本。
答案 1 :(得分:0)
number = int(input('Enter a nine digit integer: '))
if len(number) == 9:
temp = int(number)
totalSum = 0
i = 8;
while i>=0:
digit = temp//(10**i)
multiplied = digit * (i+2)
print(str(digit) + ' * ' + str(i+2) + ' = ' + str(multiplied))
totalSum += multiplied
temp -= digit*(10**i)
i -= 1
print(totalSum)
if totalSum % 11 == 1:
isbn = number + 10
print(isbn)
elif totalSum % 11 == 2:
isbn = number + 9
print(isbn)
elif totalSum % 11 == 3:
isbn = number + 8
print(isbn)
elif totalSum % 11 == 4:
isbn = number + 7
print(isbn)
elif totalSum % 11 == 5:
isbn = number + 6
print(isbn)
elif totalSum % 11 == 6:
isbn = number + 5
print(isbn)
elif totalSum % 11 == 7:
isbn = number + 4
print(isbn)
elif totalSum % 11 == 8:
isbn = number + 3
print(isbn)
elif totalSum % 11 == 9:
pisbn = number + 2
print(isbn)
elif totalSum % 11 == 10:
isbn = number + 1
print(isbn)
else:
print('Error')