Python方法传参TypeError:convertTrain()无位置参数却传入1个排查
字符替换脚本报错排查
原需求与现有代码
我正在编写一个遍历字符串并替换指定字符的脚本,思路很简单,伪代码如下:
Input1 = '' Input2 = '' Input3 = '' rawPw = Input1 + Input2 + Input 3 Remove spaces if any exist Creates a new empty str called finalPw Scan the rawPw string one character at a time. Each character goes to a Random bool and if True it goes to get converted. If false it appends finalPw In it goes to conversion it checks against a list of specified characters and if it matches it goes to a specific converter method. Otherwise it will just swap upper/lower case and append finalPw The specific converter method will use a predefined list of characters that it can be replaced by, and will use random.choice() to pick from that list to replace the character and append finalPw.
目前我已完成的代码如下,注:大小写交换功能暂未实现,我正在逐模块构建测试,上一个350行的原型完全不可用,因此本次从头开发。
import random print("Enter 3 words or series of numbers or both, each entry must be at least 5 characters in length") def main(): input1 = 'Jim' input2 = 'Samantha' input3 = 'Ethan' rawPw = (input1 + input2 + input3) refinedPw = rawPw.replace(' ', '') print(refinedPw) finalPw = '' convertTrain(refinedPw) def switch(): switchVal = random.choice(True, False) return switchVal def convertTrain(): temp = main.refinedPw onOff = False for i in temp: switch(i) if i == True: if i == 'i': the_iExc(i) else: main.finalPw.append(i) def the_iExc(): rep_iExc = ['i', '!'] repVal = random.choice(rep_iExc) for i in len(main.refinedwPw): slice(i) if i == 'i': i.replace(repVal) return i main()
收到的报错内容:
line 15, in main convertTrain(refinedPw) TypeError: convertTrain() takes 0 positional arguments but 1 was given
我已经做了多轮调整尝试修复位置参数问题,甚至移除了所有类只用无类结构编写,但是始终无法解决参数数量不匹配的问题,请求协助排查。
问题排查与修复
直接报错原因
你定义convertTrain函数时没有声明接收参数,但是在main函数中调用时传入了refinedPw,参数数量不匹配,因此抛出该错误。
其他隐藏问题
除了参数问题外,你的代码还有多个语法和逻辑错误:
random.choice需要传入可迭代对象作为参数,random.choice(True, False)写法错误,应改为random.choice([True, False])- 函数内部的局部变量无法通过
函数名.变量名的方式直接访问,main.refinedPw、main.finalPw这类写法无效,应该通过参数传递、返回值接收的方式处理变量 - 字符串是不可变类型,没有
append方法,拼接字符串需要用+=运算符 - 变量名拼写错误:
refinedwPw多写了一个w,应该为refinedPw switch函数调用后没有接收返回值,直接判断字符i == True逻辑完全错误the_iExc函数定义时没有声明形参,调用时传入i也会触发参数不匹配错误- 遍历长度需要搭配
range使用,for i in len(xxx)写法错误,应改为for i in range(len(xxx))
修复后可运行代码
import random # 定义字符替换映射表,后续新增替换规则直接在这里加即可 replace_map = { 'i': ['i', '!'], # 其他字符的替换规则可以继续补充 } def get_random_switch(): # 随机返回布尔值 return random.choice([True, False]) def convert_char(c): # 字符转换逻辑 if c in replace_map: return random.choice(replace_map[c]) # 后续可以在这里补充大小写交换逻辑 return c def convert_train(refined_pw): final_pw = '' for c in refined_pw: # 随机判断是否要转换当前字符 if get_random_switch(): final_pw += convert_char(c) else: final_pw += c return final_pw def main(): print("Enter 3 words or series of numbers or both, each entry must be at least 5 characters in length") # 测试用固定输入,后续可以改成input()接收用户输入 input1 = 'Jim' input2 = 'Samantha' input3 = 'Ethan' raw_pw = input1 + input2 + input3 refined_pw = raw_pw.replace(' ', '') print(f"处理前的字符串:{refined_pw}") final_pw = convert_train(refined_pw) print(f"处理后的密码:{final_pw}") if __name__ == "__main__": main()
内容的提问来源于stack exchange,提问作者KyleC
相关产品推荐
相关产品推荐

