'Python- print() code running when its not supposed to
Disclaimer: I am VERY inexperienced in python(if you couldn't tell by the question)
I made this basic code where you type in a given password. Once you type it in, you can either take a test or change the password. Each choice leads to different dialogue options, and so on(I used a lot of if: else and while: statements). However, for my code, after getting the test question right, it'll print the "wrong" message WITH the "you got it right" message. Then, when signing in again, you have to type in the password TWICE before it registers your input. Anyone know how to fix this? I'm using Pycharm btw
Code:
k = 1
t = 1
s = 1
passcodes = ["stanley"]
while s == 1:
while k == 1:
ans = input("Enter your password: ")
if ans in passcodes:
while t <= 10:
print("Logging in.. [" + "*" * t + (10 - t) * " " + "]")
t = t + 1
if t > 10:
print("Logged in")
k = 2
else:
print("ACCESS DENIED")
while k == 2:
ans2 = input("Take test(1) or change password(2)? ")
if ans2 == "1":
ans3 = input("What is the square root of 4? ")
if ans3 == "2":
print("Correct. Thanks for participating!")
k = 1
t = 1
if ans3 == "-2":
print("Correct. Thanks for participating!")
k = 1
t = 1
else:
print("Wrong answer. Thanks for participating!")
k = 1
t = 1
if ans2 == "2":
change = input("New password: ")
passcodes.clear()
passcodes.append(change)
print("New password " + change + " saved. Please sign back in.")
k = 1
t = 1
ans = input("Enter your password: ")
else:
print("Signing out..")
Error message:

Solution 1:[1]
hi there you have a small mistake in the sequence of the if statement when the ans3 is 2 the program rechecks if ans3 is -2 and it finds it false so it runs the code in the else statement to fix this you simply need the program to run else just in case that the two if statements are false this is achieved by using elif statement in the line " if ans3 == "-2":" it should be "elif ans3 == "-2":"
Solution 2:[2]
In this code here you have a small mistake in last if statement. In last if statement you use 'ans' statement its not needed bcz already you use this for printing a new answer so try my code and run it.its give proper output what ever you need.
k = 1
t = 1
s = 1
passcodes = ["stanley"]
while s == 1:
while k == 1:
ans = input("Enter your password: ")
if ans in passcodes:
while t <= 10:
print("Logging in.. [" + "*" * t + (10 - t) * " " + "]")
t = t + 1
if t > 10:
print("Logged in")
k = 2
else:
print("ACCESS DENIED")
while k == 2:
ans2 = input("Take test(1) or change password(2)? ")
if ans2 == "1":
ans3 = input("What is the square root of 4? ")
if ans3 == "2":
print("Correct. Thanks for participating!")
k = 1
t = 1
if ans3 == "-2":
print("Correct. Thanks for participating!")
k = 1
t = 1
else:
print("Wrong answer. Thanks for participating!")
k = 1
t = 1
if ans2 == "2":
change = input("New password: ")
passcodes.clear()
passcodes.append(change)
print(" New password " + change + " saved. Please sign back in.")
k = 1
t = 1
else:
print("Signing out..")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ahmed Yasser |
| Solution 2 | Shubh Patel |
