If statement If语句跳过第一个条件。尝试遵循1-4个步骤
我试图建立一个虚拟投票亭,接受用户id加密,验证和验证选民id,然后检查选民是否已经投票。但它跳过了第一个条件。我不是一个专业的程序员。我是新手,正在学习python。我需要你的帮助!提前感谢您的专业知识 以下是步骤:If statement If语句跳过第一个条件。尝试遵循1-4个步骤,if-statement,conditional-statements,If Statement,Conditional Statements,我试图建立一个虚拟投票亭,接受用户id加密,验证和验证选民id,然后检查选民是否已经投票。但它跳过了第一个条件。我不是一个专业的程序员。我是新手,正在学习python。我需要你的帮助!提前感谢您的专业知识 以下是步骤: 步骤1:投票者生成一对私钥和公钥-用于数字签名。投票者使用其私钥对其请求进行签名,并使用CLA的公钥发送其消息。消息必须包括投票者的签名id,例如SSN(请求)和投票者的公钥。CLA和CTF的公钥可以在投票人开始会话时传递给他/她 步骤2:CLA使用其私钥读取步骤1发送的消息,并
从代码的角度看,您似乎在将decrypt_id与文件中的所有内容进行比较,其中包含的不仅仅是decrypt_id数据。因此,由于数据不同,将永远不会执行if语句。当您使用file.read()时,您将读取文件的全部内容,您以前使用
str(encdata)
、decrpyt\u id
和str(encdata\u 2)
编写了该文件。为了便于解释,假设encdata
为1,decrypt\u id
为2,encdata\u 2
为3verify_vote
将是一个包含“123”的字符串,因为这是您编写的,decrypt_id
将是一个包含“2”的字符串。if语句计算为true的唯一情况是encdata
和encdata_2
都是空字符串
编辑:再仔细看看这段代码,if语句的目的似乎是防止用户多次投票。您可能要考虑的是,创建一个清单,该代码是“代码>解密密码Is/COD>,在每次验证一个投票人的ID时添加到IF语句中,如果在未经验证的列表中解密,则生成IF语句<代码>:
因此,if块将成为
if decrypt_id not in verified_list:
print '\n'
print 'Your voter id is verified!'
print '\n'
Vote = raw_input('Please place your vote: ')
file = open('ctf.txt', 'a') #open cla file (as a server)
file.write(str(publickey)) #writes publickey to cla file
file.write('\n')
file.write(Vote)
verified_list.append(decrypt_id)
else:
print '\n'
print 'You have already voted! Thank you!;
print '\n'
另一件需要注意的事情是,您打开cla.txt进行阅读,但从未关闭它。您应该在
验证投票=file.read()之后放置一个文件.close()
,谢谢您的帮助,我将尝试列表。
if decrypt_id not in verified_list:
print '\n'
print 'Your voter id is verified!'
print '\n'
Vote = raw_input('Please place your vote: ')
file = open('ctf.txt', 'a') #open cla file (as a server)
file.write(str(publickey)) #writes publickey to cla file
file.write('\n')
file.write(Vote)
verified_list.append(decrypt_id)
else:
print '\n'
print 'You have already voted! Thank you!;
print '\n'