Im stuck!!!

This is the place for queries that don't fit in any of the other categories.

Im stuck!!!

Postby Im Anonymous » Fri Jun 07, 2013 1:37 am

Hi guys i have been coding now for 2 days and i am just getting to grips with the basics. I have been messing around with the notepad/GUI and i am trying to create a simple machine that allows you to enter your name + set up a password. The bit where i'm getting stuck is i'm not really sure how to make it so that if the two passwords they entered do not match it will loop back to the "Please create a new password: " bit over and over again until they have entered two correct passwords :/

Code: Select all
enter = ("")

first_name = input("Please type your first name then press 'enter': ")
last_name = input("Okay "+first_name+", now can i have your last name: ")
input (enter)
print ("Hello "+first_name+" "+last_name+" my name is Luke0.1")
print ("I can navigate you anywhere from here.")
input (enter)
#
password = input("Please create a password: ")
password2 = input("Please retype your password: ")
if password == password2:
    print ("Great "+first_name+" "+last_name+" your information has been stored")
elif password != password2:
    print ("Both the passwords you entered did not match")

Im Anonymous
 
Posts: 9
Joined: Fri Jun 07, 2013 1:29 am

Re: Im stuck!!!

Postby metulburr » Fri Jun 07, 2013 3:58 am

The bit where i'm getting stuck is i'm not really sure how to make it so that if the two passwords they entered do not match it will loop back to the "Please create a new password: " bit over and over again until they have entered two correct passwords :/

It would be some sort of loop

Code: Select all
import getpass

while True:
   pass1 = getpass.getpass('Enter Password: ')
   pass2 = getpass.getpass('Enter Password again: ')
   if pass1 == pass2:
      print('passwords match')
      break
   else:
      print('passwords did not match')
      continue
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Im stuck!!!

Postby Im Anonymous » Fri Jun 07, 2013 10:45 am

Thanks a lot this really helped the only thing is, I don't really understand the code.
Im Anonymous
 
Posts: 9
Joined: Fri Jun 07, 2013 1:29 am

Re: Im stuck!!!

Postby metulburr » Fri Jun 07, 2013 11:59 am

New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Im stuck!!!

Postby sc25893 » Sat Jun 08, 2013 11:40 am

Im Anonymous wrote:Thanks a lot this really helped the only thing is, I don't really understand the code.


Code: Select all
#import the 'getpass' module which makes the getpass.getpass() function
#available for you to use in your following code
import getpass

#loop will continue until break statement is reached i.e. if pass1 is the same as pass2
while True:

    #prompts for a password, this module hides what the user types http://pymotw.com/2/getpass/
    pass1 = getpass.getpass('Enter Password: ')

    #again, a prompt with hidden user entry
    pass2 = getpass.getpass('Enter Password again: ')

    #if the user has typed the same thing twice (from above i.e. this if statement equals True)
    if pass1 == pass2:

        #print to the screen 'passwords match'
        print('passwords match')

        #break out from the loop, passwords match, all is good (else statement below is ignored)
        break

    #if pass1 does not equal pass2 (i.e. the if statement above was equal to False)
    else:

        #print to the screen 'passwords did not match'
        print('passwords did not match')

        #final line within the loop, else statement has been executed,
        #passwords did not match, stays in loop, goes back to 'Enter Password: '
        continue



I am a Python beginner myself and I can appreciate searching google does not always help you with the specific example you are stuck on. I feel lmgtfy is not helpful in this case. Maybe this will get you on your way.
sc25893
 
Posts: 23
Joined: Sat May 18, 2013 2:51 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], Nix0101, W3C [Linkcheck] and 3 guests