2016-11-07 76 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

这是我的副本:( –