2013-08-04 47 views
0

這裏是我的代碼(抱歉亂碼):爲什麼我的程序添加(「」,'我的文件的名稱

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區域的語法,但因爲我還是新的,我不知道「看不到這個問題,我個人很希望能有一個比較有經驗的程序員幫助我

+0

該程序在哪裏添加什麼? – Hyperboreus

+3

當你使用'if __name__ =='__main __''時,你應該把所有東西,除了導入,類定義和函數定義放在'main'中。它應該阻止您的代碼在作爲模塊導入時被當作腳本處理。 – user2357112

+3

請正確指定您的變量和功能,否則當您30歲時,請查看您寫入13歲的代碼,您會對自己感到非常慚愧,相信我 - 曾經在那裏:D –

回答

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

是完全無用的。

+0

謝謝先生我對於這樣的對手問題感到抱歉 –

+0

謝謝我幾年前開始使用python,但是我一直在努力處理一些更先進的東西,我真的很感激它 –

+0

define「不起作用」:DI改變了名字有些變量可以讓你理解你的問題,但是我還沒有爲你寫出整個函數。 –

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

+0

謝謝你的主席/我會採取你的建議,但一件事....我只知道如何使用'os.system'你如何使用'subprocess' –

+0

@ user2649920它有[examples](http://docs.python.org/2/library/subprocess.html#replacing-os-system) [documentation](http://docs.python.org/2/library/subprocess.html);我還添加了一個問題的鏈接。 – glglgl

相關問題