-1

I am learning the basics of programming using Python and I am trying to compare a variable to another. Here's what I have:

code = "03"
count = 0

npw=str(input("Enter code: "))
while count != 2:
    if npw == code:
        print("Success")
        break
    else:
        print("incorrect")
        npw=str(input("Enter code: "))
        count += 1
print("Reached Maximum Tries")

I want the user to have 3 tries to guess the code, but upon trying 3 tries, the third one was not read. Also, when I entered the correct code, it also prints the "Reached Maximum Tries". Thanks in Advance.

veryap19
  • 1
  • 1

1 Answers1

0
  • You check if the code is correct before asking for the last time! Change the order around to fix that problem.
  • You always print "Max". You can do that using else. See this question for examples.
  • Minor point: you don't need to convert strings to strings!

Final solution:

code = "03"

for count in range(3):
    npw = input("Enter code")
    if npw == code:
        print("Success")
        break
    print("Incorrect")
else:
    print("Max")
Jasmijn
  • 9,370
  • 2
  • 29
  • 43
  • 1
    Thank you for your help Ma'am! I am still learning the ropes and I find that this is challenging with my age (40) and my learning curve is not the same when I was young. :) I have been thinking too hard not knowing that using "For" instead of "While" will do the trick. Thanks again – veryap19 Oct 27 '20 at 23:01