Help Please :)

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

Help Please :)

Postby Im Anonymous » Mon Jun 10, 2013 8:43 pm

Okay so i have just started coding in general and i have written this code underneath, i was just wondering if there was a shorter way or easier way of writing this. If there is please could someone write this as a easier or more readable code for me so i can revise it and learn from it, thanks.

Code: Select all
#
enter = ('')
#
print ("Please press 'Enter' to continue...")
raw_input(enter)
first = raw_input("Please type your first name: ")
second = raw_input("Okay "+first+" enter your second name: ")
print("")
#
user = (first+' '+second)
print raw_input("Your username is: "+user)
#
passwords = True
while(passwords == True):
    pass1 = raw_input("Please create a password: ")
    pass2 = raw_input("Please retype your password: ")
    if pass1 == pass2:
        print("")
        print ("You have successfully created an account,")
        print ("you have logged in as "+user+ ".")
        raw_input(enter)
        passwords = False
        repass = True
        while(repass == True):
            command = raw_input(user+ ": ")
        break
    else:
        print("")
        print ("sorry "+user+" The passwords did not match,")
        print ("please press 'Enter' to try again.")
        raw_input(enter)
        continue
Im Anonymous
 
Posts: 9
Joined: Fri Jun 07, 2013 1:29 am

Re: Help Please :)

Postby metulburr » Mon Jun 10, 2013 9:13 pm

There are a couple ways to that:

Code: Select all
while True:
    if some_condition:
        break

Code: Select all
run = True
while run:
    if some_condition:
        run = False


These are two ways of breaking out of a while loop, you could do either or, but you have both. And you dont need both methods. There is also no need for continue unless you have code that you do not want to have run if that block is executed.

the line:
Code: Select all
while(passwords == True):

For one you do not need parenthesis. Secondly, you do not to make this test. You could do while passwords: or while True:, doing that way is more pythonic.

This part:
Code: Select all
        while(repass == True):
            command = raw_input(user+ ": ")

is an infinte loop. It is never going to end. There is no break, or there is no condition set repass to False. Again you do not need parenthesis around the test in Python.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1389
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Help Please :)

Postby Im Anonymous » Mon Jun 10, 2013 9:17 pm

Thanks for taking your time and reading and helping me, i really appreciate it. But i do and i don't understand what you have wrote how exactly would i go about implementing this code into mine?
Im Anonymous
 
Posts: 9
Joined: Fri Jun 07, 2013 1:29 am

Re: Help Please :)

Postby Yoriz » Mon Jun 10, 2013 9:21 pm

Code: Select all
def enterToContinue():
    raw_input("Please press 'Enter' to continue...")

enterToContinue()
first = raw_input("Please type your first name: ")
second = raw_input("Okay {} enter your second name: ".format(first))
userName = " ".join((first, second))
print "\nYour username is: {}".format(userName)

while True:
    pass1 = raw_input("Please create a password: ")
    pass2 = raw_input("Please retype your password: ")
    if pass1 == pass2:
        print "\nYou have successfully created an account,"
        print "you have logged in as {}.".format(userName)
        enterToContinue()
        break
    else:
        print "\nsorry {} The passwords did not match,".format(userName)
        enterToContinue()

command = raw_input(userName+ ": ")
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 781
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Help Please :)

Postby Im Anonymous » Mon Jun 10, 2013 9:25 pm

Thank you so much this has helped me a lot and i now understand a little bit more!
Im Anonymous
 
Posts: 9
Joined: Fri Jun 07, 2013 1:29 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests