-2

I have my password and all, but I want to have a following part of the code that you cannot access if you don't have the password right. How do I do that?

attempts=0
while attempts<3:
username=input('Username?')
password=input('Password?')
birhdate=input("Birthdate?")
pin=input("Enter your four digit pin.")
if username=='l.urban'and password=='lucasur2'and birhdate=='857585'and 
pin=='1973':
    print('you are in!')
else:
    attempts+=1
    print('incorrect!')
    if attempts==3:
        print('too many attempts')
          end code

 else:
    attempts+=1
    print('incorrect!')
    if attempts==3:
        print('too many attempts')
          end code

Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux File "main.py", line 14 `enter code here end code ^``IndentationError: unexpected indent îș§

Lucas Urban
  • 627
  • 4
  • 15
  • Indentation errors stem from wrong indentation. You might want to check the [basics of python](https://www.python-course.eu/python3_blocks.php) and how to indent properly. Moreover, watch out for blank spaces that need to be added (before `and`) as well as that "end code" is not an a python statement (`break` would be the right choice) – trotta Jul 01 '19 at 15:29
  • 1
    I think `sys.exit(1)` is probably more appropriate than `break` here, looking at how this is structured. The structure is perhaps something to address later. – SpoonMeiser Jul 01 '19 at 15:31
  • What is `end code` supposed to be doing? – Sayse Jul 01 '19 at 15:33

1 Answers1

2

Be sure to indent your code correctly, otherwise Python won't be able to parse it and you'll get IndentationError.

Your code should work once you fix indentation, but here's a simplified version if you want to study:

def check_user(username: str, password: str) -> bool:
    return username == 'admin' and password == 'password'


def main():
    tries = 1
    while True:
        if tries > 3:
            print('Too many attempts')
            return

        username = input('username?')
        password = input('password?')

        if check_user(username, password):
            break
        else:
            print('Invalid credentials')
            tries += 1
            continue
    print("You're in")
    # do some work


if __name__ == '__main__':
    main()

abdusco
  • 9,700
  • 2
  • 27
  • 44