-1
def SignupDetails(): 

  Name= input("enter name: ")
  Age= int(input("enter your age: "))
  yeargroup= input("Enter your year group: ")
  username= Name[0:3]+str(Age)
  print("username: ",username)
  Password= input("please enter a password: ")
  Password2= input("re-enter your password to confirm: ")
  if Password != Password2:
    print("the passwords do not match")

i want to create a while loop that will make the user keep entering a password until both Password and Password2 match. Any help will be appreciated

Coder777
  • 23
  • 1
  • 2
  • 8

1 Answers1

1

Try this:

Password= input("please enter a password: ")
Password2= input("re-enter your password to confirm: ")

while Password != Password2:
    print("the passwords do not match")
    Password2= input("re-enter your password to confirm: ")

EDIT

You can extend this to enable the user to re-enter the first password as well, with something like this :

Password= input("please enter a password: ")
Password2= input("re-enter your password to confirm: ")

while Password != Password2:
    Password2 = input("The passwords do not match, hit enter to start again or re-enter your previous password to confirm: ")
    if Password2 == "":
        Password= input("please enter a password: ")
        Password2= input("re-enter your password to confirm: ")
Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
  • 1
    that's not really useful if you mistyped the first password.. –  Oct 14 '17 at 19:35
  • thank you so much! sorry but is there any way to call this function without having to write SignupDetail() on the shell? – Coder777 Oct 14 '17 at 19:35
  • why? is there something more optimum or efficient i could use? – Coder777 Oct 14 '17 at 19:36
  • @efkin OP didn't mention this behaviour :) If he'll, the answer will surely get updated accordingly :) and btw this answers what is intended to be asked in the question – Ashish Ranjan Oct 14 '17 at 19:38
  • it's not about efficiency, it's about usability... imagine you type first "hllo" but you intended to write "hello", then in the second input you type "hell" but you realize that is a mismatch and next time you type "hello". you would think that it should work and would be trapped in confusion... –  Oct 14 '17 at 19:39
  • @efkin i updated my code. do you think this is better? – Coder777 Oct 14 '17 at 19:52
  • asking both the passwords again isn't a good option @Coder777, you can try something like shown in the updated answer i.e, you should give user the choice to re-enter the first password in case they want to. – Ashish Ranjan Oct 14 '17 at 19:53
  • @AshishRanjan, thx for including comments in your answer! –  Oct 14 '17 at 19:57
  • @Coder777 glad to help, it would be great if you could mark the answer as correct. It helps future visitors looking for similar answers :) – Ashish Ranjan Oct 14 '17 at 21:08