身份验证代码中的变量似乎没有添加

时间:2019-09-16 07:26:42

标签: python

它的课程。我知道这可能是最糟糕的方式,但我需要这样做。该代码通过查看用户输入的用户名和密码是否与预定义的详细信息(saveduser1)等匹配来工作,然后在变量“ authnumber”中加1。如果所有详细信息都是正确的,那么它将使authnumber等于4。出于某种原因,当我在末尾返回authnumber时,它似乎没有添加任何内容。


import random
import time
saveduser1 = ("user1")
savedpass1 = ("pass1")
saveduser2 = ("user2")
savedpass2 = ("pass2")
authnumber = 0

print ("Player one, please input your username.")
player1user = str(input())
print ("player one, please input your password.")
player1pass = str(input())
print ("Player two, please input your username.")
player2user = str(input())
print ("player one, please input your password.")
player2pass = str(input())

if player1user == saveduser1:
    authnumber + (1)


else:
    authnumber = 0



if player1pass == savedpass1:
    authnumber + (1)
else:
    authnumber + (10)

if player2user == saveduser2:
    authnumber + (1)
else:
    authnumber + (0)

if player2pass == savedpass2:
    authnumber + (1)
else:
    authnumber + (0)

if authnumber >= (4):
    print ("opened")


else:

    print (authnumber)

2 个答案:

答案 0 :(得分:0)

我们需要做:

authnumber += x    # Or: authnumber = authnumber + x

代替:

authnumber + x     # This just calculates "authnumber + x", but does not assign it to "authnumber".

答案 1 :(得分:0)

您需要为其自身重新分配authnumber才能更改其值。还要删除代码中的多余括号。

import random
import time
saveduser1 = "user1"
savedpass1 = "pass1"
saveduser2 = "user2"
savedpass2 = "pass2"
authnumber = 0

print ("Player one, please input your username.")
player1user = str(input())
print ("player one, please input your password.")
player1pass = str(input())
print ("Player two, please input your username.")
player2user = str(input())
print ("player one, please input your password.")
player2pass = str(input())

if player1user == saveduser1:
    authnumber += 1


else:
    authnumber = 0



if player1pass == savedpass1:
    authnumber += 1
else:
    authnumber += 10

if player2user == saveduser2:
    authnumber +=1
else:
    pass

if player2pass == savedpass2:
    authnumber += 1
else:
    pass

if authnumber >= (4):
    print ("opened")


else:

    print (authnumber)