Python 负整数值错误,追加但不写入

Python 负整数值错误,追加但不写入,python,file,integer,append,Python,File,Integer,Append,我的第一个问题是,当我输入tex-10时,Exception ValueError不会运行,只有第一个输入会再次运行,但如果我输入-10,则ValueError会运行。我希望ValueError在输入-10时运行,这是一个没有参数的负数 我的第二个问题是,这个函数只在我有一个,append时才起作用,而在我写的时候不起作用,有人知道为什么,以及我如何修复它吗?我想写一个文件 def list_to_file(): file=open("file.txt&quo

我的第一个问题是,当我输入tex-10时,Exception ValueError不会运行,只有第一个输入会再次运行,但如果我输入-10,则ValueError会运行。我希望ValueError在输入-10时运行,这是一个没有参数的负数

我的第二个问题是,这个函数只在我有一个,append时才起作用,而在我写的时候不起作用,有人知道为什么,以及我如何修复它吗?我想写一个文件

def list_to_file():
             file=open("file.txt","a") 
             file.write("\n")
             file.write("".join(str(lista)))

您的try:except:语句格式设置错误。应该是这样的:

try:
    1/0
except:
    print("the exception happened")
#your code
while True:
       try:
          number = int(input("State the number of latitudes you want to calculate energy for?"))
       if number>0:
            break
except ValueError: 
           print("That was not a positive number") ##
def list_to_file(count, lista):
    file=open("file_"  + str(count) + ".txt","w") 
    file.write("\n")
    file.write("".join(str(lista)))
    file.close()

allLists = list of all your listas
count = 1
for thisList in allLists:
    list_to_print(count, thisList)
    count +=1
注意try是如何直接在上面的,除了。。。在您的中,它看起来是这样的:

try:
    1/0
except:
    print("the exception happened")
#your code
while True:
       try:
          number = int(input("State the number of latitudes you want to calculate energy for?"))
       if number>0:
            break
except ValueError: 
           print("That was not a positive number") ##
def list_to_file(count, lista):
    file=open("file_"  + str(count) + ".txt","w") 
    file.write("\n")
    file.write("".join(str(lista)))
    file.close()

allLists = list of all your listas
count = 1
for thisList in allLists:
    list_to_print(count, thisList)
    count +=1
在while语句下面有一个except语句,它没有属于它的try语句

您可能希望这样做的方式大致如下:

while True:
    try:
        #some sort of statement here that is going to throw an exception like 1/0 ( cant divide by zero bro )
    except:
        print("cant divide b zero bro")
        #some statement that you want to happen after the above exception is thrown
        break
除此之外,break不是一个异常诱导语句,它所做的只是阻止循环结束。表示break的行应该是这样的

while True:
    try:
        if _some_sort_of_logic_not_fufilled:
            raise Exception("some exception text if you want") #you could use your ValueError here also since ValueError is derived from Exception
    except:
        print "see how it got here?"
        break # this would go here if you want it to STOP the loop after it gets the exception, or dont put break if you want it to keep going... but a while True loop... you probably wnat to eventually break out of...

对于第一个问题,听起来像是要引发一个值错误,然后将其排除

number = int(input("Area?"))
while True:
    try:
        if number > 0:
            break
        else:
            raise ValueError()
    except ValueError:
        number = int(input("Please enter a positive number"))
至于第二个问题,当它是一个小列表时,我无法写入文件,但当它是大的时,我能够写入文件。我在这里提出了这个问题,得到了一个有效的答案

简言之,在完成对文件的写入后,必须使用file.close关闭该文件

def list_to_file():
     file=open("file.txt","w") 
     file.write("\n")
     file.write("".join(str(lista)))
     file.close()
因此,如果你有同样的问题,这应该做的把戏

要为每次迭代创建一个新文件,请在每次迭代中创建一个递增的计数,并将其发送到打印方法。然后使用该计数生成一个唯一的文件名,如下所示:

try:
    1/0
except:
    print("the exception happened")
#your code
while True:
       try:
          number = int(input("State the number of latitudes you want to calculate energy for?"))
       if number>0:
            break
except ValueError: 
           print("That was not a positive number") ##
def list_to_file(count, lista):
    file=open("file_"  + str(count) + ".txt","w") 
    file.write("\n")
    file.write("".join(str(lista)))
    file.close()

allLists = list of all your listas
count = 1
for thisList in allLists:
    list_to_print(count, thisList)
    count +=1

break不等于raise exception那不就是代码重复吗?我能用另一种方法解决这件事吗@泰特瑞索,等等,我知道你在做什么。。。让我写一个答案。。。但是在我写这篇文章的时候,请纠正你的空白。按照你上次给出的例子,这仍然不正确@多亏了你,泰特瑞斯公司才得以运作!,你举例说明了如何完美地修复它:@Malonge你对另一个问题有什么线索吗?我正在解决这个问题,尽管奇怪的是我也遇到了麻烦。如果你觉得我的答案有用的话,你应该点击它上面的向上箭头。我试图做的事情是将一个表写入一个文件,但问题是我一直在向该文件添加和添加结果,我想每次都写入,所以我想改为使用w,但它不起作用。。如果你能弄明白,那就太棒了!无论如何谢谢@MalongeInteresting。我只是让它为我工作。我只是想澄清一下,您是想每次更新相同的旧文件还是为每次迭代创建一个新文件?我想为每次迭代创建一个新文件,但我不能这样做。我只能更新相同的旧文件,如何解决此问题?