为什么我的程序添加('','到我的文件的名称?

时间:2013-08-04 06:12:24

标签: python python-3.x

这是我的代码(对于凌乱的代码感到抱歉):

def main():
    pass

if __name__ == '__main__':
    main()
from easygui import *
import time
import os
import random
import sys

##multenterbox(msg='Fill in values for the fields.', title=' ', fields=(), values=())
msg = "Enter your personal information"
title = "Credit Card Application"
fieldNames = ["First name",'Last name','email',"Street Address","City","State","ZipCode",'phone','phone 2)']
fieldValues = []  # we start with blanks for the values
fieldValues = multenterbox(msg,title, fieldNames)
# make sure that none of the fields was left blank
def make(x):
    xys = x,".acc"
    xyzasd = str(xys)
    tf = open(xyzasd,'a+')
    tf.writelines(lifes)
    tf.writelines("\n")
    tf.writelines("credits = 0")
    tf.close
def add(x):
    nl = "\n"
    acc = ".acc"
    xy = x + acc
    exyz = xy
    xyz = exyz
    xxx = str(xyz)
    tf = open('accounts.dat',"a+")
    tf.writelines(nl)
    tf.writelines(xxx)
    tf.close

while 1:
    if fieldValues == None: break
    errmsg = ""
    for i in range(len(fieldNames)-1):
        if fieldValues[i].strip() == "":
            errmsg += ('"%s" is a required field.\n\n' % fieldNames[i])
    if errmsg == "":
        break # no problems found
    fieldValues = multenterbox(errmsg, title, fieldNames, fieldValues)
names = enterbox(msg= ('confirm FIRST name and the FIRST LETTER of the persons LAST name'))
##txt = "acc"
##na = str(name)
##name = (names)

life = ( str(fieldValues))
lifes = life,'\n'
herro = ("Reply was: %s" % str(fieldValues))
correct  = buttonbox(msg=(herro,'\n is that correct'),choices = ('yes','no','cancel'))


if correct == "yes":
    make(names)
    add(names)
elif correct == "no":
    os.system('openacc.py')
    time.sleep(0.5)
    sys.exit()
else:
    os.system('cellocakes-main.py')
    sys.exit()
os.system('cellocakes-main.py')

我不知道问题是什么我也很抱歉它是如何草率编程我有一个白板帮助我仍然新编程(我只有13)对不起。我个人认为这个问题出在def add area的语法中,但因为我还是新手,我没有亲自看到这个问题,我希望有一个更有经验的程序员帮助我。

2 个答案:

答案 0 :(得分:2)

这不是直接回答你问题的答案。

唉,评论字段仍然无法保存格式化代码,因此我选择这种方式。

def main():
    pass

if __name__ == '__main__':
    main()

这是一个很好的编码模式,但是你以无用的方式使用它。

如果它作为模块导入而不是作为脚本执行,则应该阻止执行这些东西。

然而,总是使用它并不坏,但是然后将代码放在main()函数中而不是在下面添加它。

fieldNames = ["First name",'Last name','email',"Street Address","City","State","ZipCode",'phone','phone 2)']

)太多了。

fieldValues = []  # we start with blanks for the values
fieldValues = multenterbox(msg,title, fieldNames)

第二行使第一行无用,因为你不在中间使用fieldValues

如果您希望multenterbox()失败并希望[]作为默认值,则会有所不同。

def make(x):
    xys = x,".acc"
    xyzasd = str(xys)
    tf = open(xyzasd,'a+')
    tf.writelines(lifes)
    tf.writelines("\n")
    tf.writelines("credits = 0")
    tf.close

你已经被告知过:x, ".acc"创建了一个元组,而不是一个字符串。要创建字符串,请使用x + ".acc"

此外,您的close来电无法通话,因为它缺少()。这个只引用该函数并忽略该值。

更好的方法是写(请恰当地命名你的变量)

    with open(xyzs, 'a+') as tf:
        tf.writelines(lifes)
        tf.writelines("\n")
        tf.writelines("credits = 0")

即使发生错误,with语句也会自动关闭文件。

此外,您使用writelines()错误:它是supposed to take a sequence of strings并将每个元素写入文件。由于它没有在中间添加换行符,因此结果看起来相同。但在你的情况下,它会分别写入每个字节,使其效率更低一些。

此外,您还可以从函数中访问全局变量lifes。如果绝对必要,你应该只做这些事情。

def add(x):

以上同样的评论如上所述,加上

xy = x + acc
exyz = xy
xyz = exyz
xxx = str(xyz)

为什么?只需使用xy;这两个作业没有任何用处,str()调用也没用,因为你已经有了一个字符串。

for i in range(len(fieldNames)-1):
    if fieldValues[i].strip() == "":
        errmsg += ('"%s" is a required field.\n\n' % fieldNames[i])

更好:

for name, value in zip(fieldNames, fieldValues):
    if not value.strip(): # means: empty
        errmsg += '"%s" is a required field.\n\n' % name

然后:

life = ( str(fieldValues))

从列表中创建一个字符串。

lifes = life,'\n'

从这两个字符串中生成一个元组。

os.system('openacc.py')

os.system('cellocakes-main.py')

请不要使用os.system();它已被弃用。更好地使用the subprocess module

答案 1 :(得分:1)

问题的问题在于:

# assign the tuple (x, ".acc") to xys 
xys = x,".acc"

# now xyzasd is the tuple converted to a string, thus
# making the name of your file into '("content of x", ".acc")'
xyzasd = str(xys)

# and open file named thus
tf = open(xyzasd,'a+')

您想要做的是:

# use proper variable and function names!
def make_account(account):
    filename = account + '.acc'
    the_file = open(filename, 'a+')
    ....

另一方面,您的代码还存在其他问题,例如

def main():
    pass

if __name__ == '__main__':
    main()

完全没用。