python nonetype转换str_TypeError:无法将“NoneType”对象隐式转换为str

我是Python新手,所以我不明白为什么会出现以下错误:TypeError:无法将“NoneType”对象隐式转换为strimport math, os

try:

while True:

loadfile = input ("Get a string from input or file: ").lower()

if loadfile == "":

print("You need to enter whether you want the string from input or a file. Please try again")

elif loadfile[0] != "i" and loadfile[0] != "s" and loadfile[0] != "f":

print("You can only choose to get the string from input or a file. Please try again")

else:

loadfile = loadfile[0] == "f"

raise Execption

except Exception:

pass

if loadfile:

while True:

filename = input ("Enter a file name containing a string: ")

if filename == "":

print("You need to enter a file name, you can not leave it blank. Please try again")

elif not os.path.isfile(filename):

print ("This file does not exist. Please try again")

else:

try:

file = open(filename, "r")

except OSError:

print("Your operating system threw an error")

print("The file may be in use, or your filename may be invalid")

else:

string = file.read()

file.close()

if os.path.getsize(filename) == 0:

print("Found the file vut the file is empty. Enter another file")

else:

flag = False

for x in string:

if x.isalpha():

flag = True

if flag:

break

else:

print("The string inside this file does not contain a character from the alphabet. Please try again")

if (len(string) <= 200):

print ("Found the string: " + string)

else:

print("String found, but not being printed since it it greater than 200 characters")

else:

try:

while True:

string = input ("Enter your string: ");

if string == "":

print ("Invalid response. You need to enter a plaintext message to be able to continue")

else:

for x in string:

if x.isalpha():

raise Exception;

print ("You did not enter a character from the alphabet in your plaintext message. Please try again");

except Exception:

pass

string2 = ""

string3 = ""

try:

while True:

keyword = input ("Enter your KEYWORD: ");

if keyword == "":

print ("You need to enter a keyword. Please try again");

elif keyword.isalpha():

raise Exception;

else:

print ("Your keyword cannot contain any numbers, spaces, or symbols. Please try again");

except Exception:

pass

keywordRepeated = (keyword * math.ceil(len(string)/len(keyword))).upper()

try:

while True:

keyword2 = input ("Enter your second KEYWORD: ");

if keyword2 == "":

print ("You need to enter a keyword. Please try again");

elif keyword2.isalpha():

raise Exception;

else:

print ("Your keyword cannot contain any numbers, spaces, or symbols. Please try again");

except Exception:

pass

keyword2Repeated = (keyword2 * math.ceil(len(string)/len(keyword2))).upper()

try:

while True:

decryption = input("Would you like to encrypt or decrypt?: ").lower()

if decryption == "":

print("You need to enter whether you want to undergo the encryption or decryption process. Please try again")

elif decryption[0] != "d" and decryption[0] != "e":

print("You can only choose encryption or decryption. Please try again")

else:

decryption = decryption[0] == "d"

raise Exception

except Exception:

pass

try:

while True:

savefile = input("Should the string be saved ot the file, or displayed on screen?: ").lower()

if savefile == "":

print("You need to enter whether to save the string to a file or display it on screen")

elif savefile[0] != "d" and savefile[0] != "s" and savefile[0] != "f":

print("You can only choose to save the string to a file or display it on screen")

else:

savefile = savefile[0] != "d"

raise Exception

except Exception:

pass

firstuppercase = ord("A"); firstlowercase = ord("a"); lastuppercase = ord("Z"); lastlowercase = ord("z");

def encryptedChar(position, parString, parKeyword):

if not parString[position].isalpha():

return parString[position]

charnum = ord(parString[position])

modifier = ord(parKeyword[position].upper()) + 1 - firstuppercase

modifier *= -1 if decryption else 1

charnum2 = charnum + modifier

if decryption:

if charnum <= lastuppercase and charnum2 < firstuppercase:

charnum += 26

elif charnum >= firstlowercase and charnum2 < fisrtlowercase:

charnum += 26

else:

if charnum <= lastuppercase and charnum2 > lastuppercase:

charnum2 -= 26

elif charnum >= firstlowercase and charnum2 > lastlowercase:

charnum2 -= 26

for x in range(len(string)):

string2 += encryptedChar(x, string, keywordRepeated)

string3 += encryptedChar(x, string2, keyword2Repeated)

if savefile:

while True:

try:

while True:

filename = input ("Enter a filename to save to: ")

if filename == "":

print("You need to enter a file name, you cannot leave it blank")

else:

raise Exception

except Exception:

pass

try:

file = open(filename, "w")

raise Exception

except OSError:

print("Your operating system threw an error")

print("The file may be in use, or your file may be invalid")

except Exception:

file.write(string3)

file.close()

print("Save successful!")

break

else:

print(string3)

我希望代码按下面的方式运行,但我一直收到这个类型错误Get a string from input or file: i

Enter your string: letters

Enter your KEYWORD: working

Enter your second KEYWORD: code

Would you like to encrypt or decrypt?: e

Should the string be saved ot the file, or displayed on screen?: d

Traceback (most recent call last):

File "C:\Users\User\Documents\task 3.py", line 138, in

string2 += encryptedChar(x, string, keywordRepeated)

TypeError: Can't convert 'NoneType' object to str implicitly

>>>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值