15,877,168 members
1.00/5 (1 vote)
See more:
Here's a code snippet of the problem vv

Python
```<pre>
import time

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")
try:
except:
a = a
else:
a = a + 1
if a > 0:
ans = ans.lower()
if ans == 'y' or ans == 'yes':
time.sleep(1)
print("...\n")
time.sleep(1)
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)
ans = "no"
while ans == "no" or ans == "n":
ans = ans.lower()
if ans == "y" or ans == "yes":
time.sleep(1)
print("...\n")
time.sleep(1)
time.sleep(2)
f.close()
else:
print("Test failed!")
else:
time.sleep(2)
else:
print("You username cannot have a special character.")
time.sleep(2)

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

What I have tried:

Posted
Updated 18-Sep-18 6:29am
v2

## Solution 1

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":
time.sleep(1)
print("...\n")
time.sleep(1)
time.sleep(2)
f.close()
else:
print("Test failed !\n")
```

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.

## Solution 2

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.

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! :)

## Solution 3

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

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")
try:
except:
a = a
else:
a = a + 1
if a > 0:
ans = ans.lower()
if ans == 'y' or ans == 'yes':
#    time.sleep(1)
print("...\n")
#    time.sleep(1)
#    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)

ans = "no"
while ans == "no" or ans == "n":
ans = ans.lower()
if ans == "y" or ans == "yes":
#                   time.sleep(1)
print("...\n")
#                   time.sleep(1)
#                   time.sleep(2)
#                   f.close()
else:
print("Test failed!")
else:
#               time.sleep(2)
else:
print("You username cannot have a special character.")
#           time.sleep(2)

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?