我的课程自动运行的原因是什么?

huangapple go评论62阅读模式
英文:

why does my class is executing on its own?

问题

#sc.py
# 时间
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
# 类
class password_log:
    def __init__(self, **value):
        self.passwordattemptslog = value['pwdalog']
        self.maincode = value["maincode"]
        self.var_input = value["var_input"]
        self.inputpassword = value["password"]
        self.logs = value["logs"]
        self.f_obj = value["log_file"]
        self.file = open(self.f_obj, 'r+')
        self.wrongpassword_attempts = 0
    def checker(self): # 检查器
        if self.logs == True:
            if self.var_input == self.inputpassword:
                self.maincode
                # 日志
                self.correctpassword()
            elif self.var_input != self.inputpassword:
                print("错误的密码")
                self.wrongpassword()
                self.wrongpassword_attempts += 1
            elif self.wrongpassword_attempts > self.passwordattemptslog:
                self.limitpassword()
            else:
                print("错误")


        elif self.logs == False:
            if self.var_input == self.inputpassword:
                self.maincode
            elif self.var_input != self.inputpassword:
                print("错误的密码")
            else:
                print("错误")
        else:
            print("错误")

    # 日志
    def correctpassword(self):
        self.file.write("\n正确的密码" + " 时间:" + current_time + "\n")

    def wrongpassword(self):
        self.file.write("\n错误的密码" + " 时间:" + current_time + "\n")
        self.file.write("\n 错误的密码输入:" + self.var_input)
    def limitpassword(self):
        self.file.write(f"\n {self.passwordattemptslog} 次密码错误尝试时间:" + current_time + "\n")

#main.py
from sc import password_log
import getpass


class main:
    @staticmethod
    def runtest():
        print("运行测试")


log_input = getpass.getpass(prompt="密码:")

paswlog = password_log(password="2008", logs=True, var_input=log_input, maincode=main.runtest(), log_file="filetext.txt", pwdalog=10)

paswlog.checker()
英文:

so i'm working on a code but I encounter a error and idk why but I will give the details of the code

#sc.py
import getpass
import time
import subprocess
import sys
#time
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
#class
class password_log:
	def __init__(self,**value):
		self.passwordattemptslog = value['pwdalog']
		self.maincode = value["maincode"]
		self.var_input = value["var_input"]
		self.inputpassword = value["password"]
		self.logs = value["logs"]
		self.f_obj = value["log_file"]
		self.file = open(self.f_obj,'r+')
		self.wrongpassword_attempts = 0
	def checker(self):#checker
		if self.logs == True:
			if  self.var_input == self.inputpassword:
				self.maincode
						#logs
				self.correctpassword()
			elif self.var_input != self.inputpassword:
				print("wrong password")
				self.wrongpassword()
				self.wrongpassword_attempts += 1
			elif self.wrongpassword_attempts > self.passwordattemptslog:
				self.limitpassword()
			else:
				print("error")


		elif self.logs == False:
			if self.var_input == self.inputpassword:
				self.maincode
			elif self.var_input != self.inputpassword:
				print("wrong password") 
			else:
				print("error")
		else:
			print("error")

	#logs
	def correctpassword(self):
		self.file.write("\ncorrect password" +" time:" + current_time + "\n" )

	def wrongpassword(self):
		self.file.write("\nwrong password" +" time:" + current_time + "\n" )
		self.file.write("\n the wrong password input:" + self.var_input)
	def limitpassword(self):
		self.file.write(f"\n {self.passwordattemptslog} wrong attempts of password time:" + current_time + "\n" )

#main.py
from sc import password_log
import getpass


class main:
	@staticmethod		
	def runtest():
		print("runtest")





log_input = getpass.getpass(prompt = "password:")

paswlog = password_log(password = "2008", logs = True, var_input = log_input,maincode = main.runtest() , log_file = "filetext.txt", pwdalog = 10)

paswlog.checker()

output:
runtest
password:

The runtest() has already been executed, but it should only be run if the password is correct.

im just new at python and experimenting some stuff so yeah i need a little help on this one

i expect that the runtest() should only be run if the password is correct

答案1

得分: 2

你没有将runtest函数作为参数传递,而是执行了该函数并传递了其返回值。在这种情况下,返回值是None。这是基于您想要在password_log.checker中运行它并使用其返回值的假设。

log_input, maincode = main.runtest() # 错误
log_input, maincode = main.runtest   # 正确
英文:

Instead of passing the runtest function as an argument, you are executing the function and passing its return value. Which in this case in None. This is assuming you want to run it in password_log.checker and use its return value.

log_input,maincode = main.runtest() #  Wrong
log_input,maincode = main.runtest   #  Right 

答案2

得分: 1

main.py中,我的第一直觉是在main.runtest()这一行,它应该是maincode = main.runtest。否则,你会立即调用该函数并将maincode设置为它的返回值。这个函数的早期调用可能解释了为什么你的程序会立即执行,绕过密码检查。

然后,在sc.pychecker部分,无论你想在哪里调用maincode,它都应该是maincode(),否则你只是命名了该函数,而实际上没有调用它。

我无法测试这个,所以这些只是我的猜测。如果有帮助,请告诉我。

英文:

My first instinct is in main.py, on the line maincode = main.runtest(), it should be maincode = main.runtest. Otherwise you would immediately call the function and set maincode to its return value. This early call of the function probably explains why your program executes immediately bypassing the password check

Then, in sc.py in checker, wherever you want to call maincode, it should be maincode(), otherwise you would just name the function without actually calling it.

I cannot test this so those are just my guesses. Let me know if they help.

huangapple
  • 本文由 发表于 2023年6月13日 09:39:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定