Python早先关闭了。 [英] Python prematurly shuts down.

查看:75
本文介绍了Python早先关闭了。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是问题的代码片段vv





Here's a code snippet of the problem vv


<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()







因此当您回答ans问题时,程序会关闭。



我尝试了什么:



我试着到处寻找答案。我已下载并重新下载python但没有任何效果。




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.

推荐答案

Quote:

所以当你回答ans问题时程序会关​​闭。

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:

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")


我在Python 3.6和IDLE IDE中尝试了你的代码,在这两种情况下它都正确显示了一条错误信息。首先,在第一行之后你有一个额外的缩进级别。一旦你纠正了那么你就会看到你正在使用变量名用户名,而不事先声明它。
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.


以下代码似乎可以正常工作。注意我已经注释了将详细信息写入文件的部分,以及大多数睡眠调用(为什么它们都存在?)。



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?).

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()


这篇关于Python早先关闭了。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆