Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here's a code snippet of the problem vv


Python
<pre>
import time

def password():
        ans = "no"
	while ans == 'no' or ans == 'n':
		a = 0
		password = input("Enter a password. \n(Must contain at least 1 number, 1 captial, 5 characters and have no special characters)\n")
		if password.isalnum() == True:
			if password != password.lower():
				if len(password) > 4:
					for i in range(len(password)):
						try:
							float(password[i])
						except:
							a = a
						else:
							a = a + 1
					if a > 0:
						ans = input("Your password is " + password + "\nIs this the password you wanted? Y/N\n")
						ans = ans.lower()
						if ans == 'y' or ans == 'yes':
							f = open("Password.txt", "w")
							f.write(password)
							print("Your password is being added to the database.\n")
							time.sleep(1)
							print("...\n")
							time.sleep(1)
							print("Your password has been set.")
							time.sleep(2)
							f.close()
						else:
							print("Test failed!")
					else:
						print("You need at least 1 number.")
						time.sleep(2)
				else:
					print("You must have at least 5 characters.")
			else:
				print("You need at least 1 capital.")
				time.sleep(2)
		else:
			print("That contains a special character.")
			time.sleep(2)
def username():
	ans = "no"
	while ans == "no" or ans == "n":
		username = input("What do you want your username to be?\n")
		if username.isalnum() == True:
			if len(username) > 4:
				ans = input("Your username is " + username + " is this correct? Y/N\n")
				ans = ans.lower()
				if ans == "y" or ans == "yes":
					f = open("Username.txt", "w")
					f.write(username)
					print("Your username is being added to the database.\n")
					time.sleep(1)
					print("...\n")
					time.sleep(1)
					print("Your username has been set.")
					time.sleep(2)
					f.close()
					password()
				else:
					print("Test failed!")
			else:
				print("Your username has to have more than 4 characters.")
				time.sleep(2)
		else:
			print("You username cannot have a special character.")
			time.sleep(2)
username()



So the program shuts down when you answer the "ans" question.

What I have tried:

I have tried searching everywhere for an answer. I've downloaded and re downloaded python but nothing worked.
Posted
Updated 18-Sep-18 6:29am
v2

Quote:
So the program shuts down when you answer the "ans" question.

How do you know that it is not the normal behavior ?
Because 'nothing' is exactly what should happen when test fails.
Try this:
Python
ans = input("Your username is " + username + " is this correct? Y/N\n")
if ans == "Y" or ans == "y" or ans == "Yes" or ans == "yes":
    f = open("Username.txt", "w")
    f.write(username)
    print("Your username is being added to the database.\n")
    time.sleep(1)
    print("...\n")
    time.sleep(1)
    print("Your username has been set.")
    time.sleep(2)
    f.close()
else:
    print("Test failed !\n")
 
Share this answer
 
Comments
Member 13986449 16-Sep-18 20:27pm    
Thanks for replying but when I added the else statement to my code it still closed.
So whenever I run it in visual studio code ( the software I use ) it works but when I run it in python it doesn't work, it shuts down.
Patrice T 16-Sep-18 20:57pm    
There should be an error message some where.
I have tried your code in Python 3.6, and in the IDLE IDE, and in both cases it correctly shows an error message. Firstly you have an extra indent level after the first line. Once you correct that then you see that you are using the variable name username without previously declaring it.
 
Share this answer
 
Comments
Member 13986449 17-Sep-18 20:27pm    
Thank you for taking time out of your day to responding to me :)
I have updated the question to show the whole code (The code before was a snippet of where the program stopped.
Richard MacCutchan 18-Sep-18 12:24pm    
I had a look at this this morning and managed to get part of it working after fixing the indentation. Unfortunately I was not able to get any further and will most likely not be able to for the rest of this week. If I do have time I will update this.
Member 13986449 18-Sep-18 20:15pm    
Thanks! :)
The following code appears to work. Note I have commented out the parts that write the details to the file, and most of the sleep calls (why are they there?).

Python
import time

def password():
    ans = "no"
    while ans == 'no' or ans == 'n':
        a = 0
        password = input("Enter a password. \n(Must contain at least 1 number, 1 captial, 5 characters and have no special characters)\n")
        if password.isalnum() == True:
            if password != password.lower():
                if len(password) > 4:
                    for i in range(len(password)):
                        try:
                            float(password[i])
                        except:
                            a = a
                        else:
                            a = a + 1
                    if a > 0:
                        ans = input("Your password is " + password + "\nIs this the password you wanted? Y/N\n")
                        ans = ans.lower()
                        if ans == 'y' or ans == 'yes':
                        #    f = open("Password.txt", "w")
                        #    f.write(password)
                            print("Your password is being added to the database.\n")
                        #    time.sleep(1)
                            print("...\n")
                        #    time.sleep(1)
                            print("Your password has been set.")
                        #    time.sleep(2)
                        #    f.close()
                        else:
                            print("Test failed!")
                    else:
                        print("You need at least 1 number.")
                        #time.sleep(2)
                else:
                    print("You must have at least 5 characters.")
            else:
                print("You need at least 1 capital.")
#                time.sleep(2)
        else:
            print("That contains a special character.")
#            time.sleep(2)
 
def username():
    ans = "no"
    while ans == "no" or ans == "n":
        username = input("What do you want your username to be?\n")
        if username.isalnum() == True:
            if len(username) > 4:
                ans = input("Your username is " + username + " is this correct? Y/N\n")
                ans = ans.lower()
                if ans == "y" or ans == "yes":
 #                   f = open("Username.txt", "w")
 #                   f.write(username)
                    print("Your username is being added to the database.\n")
 #                   time.sleep(1)
                    print("...\n")
 #                   time.sleep(1)
                    print("Your username has been set.")
 #                   time.sleep(2)
 #                   f.close()
                    password()
                else:
                    print("Test failed!")
            else:
                print("Your username has to have more than 4 characters.")
 #               time.sleep(2)
        else:
            print("You username cannot have a special character.")
 #           time.sleep(2)

username()
 
Share this answer
 
Comments
Member 13986449 18-Sep-18 20:20pm    
It worked! Thanks :) when I deleted the file operations it worked ( somehow ).
Richard MacCutchan 19-Sep-18 3:19am    
I just added the file operations back in and it worked fine. You must be doing something else that affects your environment. Are you running it in a restricted location?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900