2016-11-07 68 views
-3
import time 
def mainmenu(): 
    print ("1.set values") 
    print ("2. run formula") 
    print ("3. export formula results") 
    maininput = int(input("Enter: ")) 
    if maininput == 1: 
     set_values() 
    elif maininput == 2: 
     formula() 
    elif maininput == 3: 
     export() 

def set_values(): 
    set_values.first = int(input("Value 1 between 1 and 10")) 
    while 1< set_values.first <10: 
     set_values.second = int(input("Value 2 between 1 and 10")) 
     while 1< set_values.second <10: 
      mainmenu() 
    else: 
     print ("That is not a valid value") 
     return set_values() 

def formula(): 
    part_1 = set_values.first + set_values.second 
    print ("Value 1 + value 2 =",part_1) 
    time.sleep(2) 
    part_2 = part_1 * 5 
    print ("Value 1 + value 2 x 5 =",part_2) 
    time.sleep(2) 
def export(): 
    print() 

mainmenu() 

我將在def export中使用哪些代碼來替換print(),以便將公式中打印的數據寫入文本文件。 在寫入數據之前,應該要求用戶輸入一個文件名,並且代碼應該檢查是否存在具有相同名稱的文件,如果有,請詢問用戶是否應該覆蓋該文件。如果用戶選擇不覆蓋文件,則應將其返回到輸入文件名的部分。在python上編寫文本文件

+0

谷歌和「蟒蛇寫入到文件」 –

+0

問這樣一個基本問題之前,你應該閱讀文檔「蟒蛇如果檢查文件是否存在」。互聯網上有成千上萬(如果不是數百萬)例子。 –

+0

@BryanOakley我做了檢查,但沒有任何匹配我的需求,我會再次檢查,如果我發現一個病態刪除這篇文章。 – Josh

回答

0

這是如何打印到一個txt文件:

file = open("Exported.txt", "w") 
file.write("Text to write to file") 
file.close() 

另一種方式這樣做將是:

with open('Exported.txt', 'w') as file: 
    file.write("Text to write to file") 

這是我做了寫一個txt文件的程序:

import os.path 

def start(): 

    print("What do you want to do?") 
    print(" Type a to write a file") 
    print(" Type b to read a file") 
    choice = input("   -") 
    if choice == "a": 
     create() 
    elif choice == "b": 
     read() 
    else: 
     print("Incorrect spelling of a or b\n\n") 
     start() 


def create(): 

    print() 
    filename = input("What do you want the file to be called?\n") 
    if os.path.isfile(filename): 
     print("This file already exists") 
     print("Are you sure you would like to overwrite?") 
     overwrite = input("y or n") 
     if overwrite == "y": 
      print("File has been overwritten") 
      write(filename) 
     else: 
      print("I will restart the program for you") 
    elif not os.path.isfile(filename): 
     print("The file has not yet been created") 
     write(filename) 
    else: 
     print("Error") 





def write(filename): 
    print() 
    print("What would you like the word to end writing to be?") 
    keyword = input() 
    print("What would you like in your file?") 
    text = "" 
    filename = open(filename, 'w') 
    while text != keyword: 
     filename.write(text) 
     filename.write("\n") 
     text = input() 


def read(): 
    print() 
    print("You are now in the reading area") 
    filename = input("Please enter your file name:  -") 
    if os.path.isfile(filename): 
     filename = open(filename, 'r') 
     print(filename.read()) 
    elif not os.path.isfile(filename): 
     print("The file does not exist\n\n") 
     start() 
    else: 
     print("Error") 


start() 
+0

將返回值直接與布爾值進行比較從來都不是一個好主意。更好的想法是簡單地使用布爾值的真實性。例如。 '如果boolean_return_value:'。 –

+0

那麼如何將其實現到我的代碼? –

+0

我將以您的代碼爲例。改變這個'if os.path.isfile(filename)== True:'to this'if os.path.isfile(filename):'。或者另一個例子,改變這個'elif os.path.isfile(filename)== False:'這個'elif not os.path.isfile(filename):' –

1

您應該參考文檔openwrite(鏈接here)。以外的是,用於寫入一個文件的首選方法如下:

with open('myfile.txt', 'w') as f: 
    f.write('Writing to files is easy') 
+0

這是我的副本:( –