如何使用PyCharm

时间:2018-12-14 15:45:01

标签: python file pycharm append

下面是我用来使用pycharm将数据输入文件的代码,但是,在代码执行后文件没有更改,我知道复制后就可以使用

    while True:
        name 
    print(name, age, position, salary, years, sep=', ')
    return name, age, position, salary, years

1 个答案:

答案 0 :(得分:0)

我看不到您追加到文件中。

以下是使用函数add_emp()附加到.txt文件(在此示例中称为 dummy.txt )的完整示例:

def add_emp():

    while True:
        name = input('Please Enter Employee Full Name: ')
        if all(name.isalpha() or name.isspace() for name in name):
            break
        print('Invalid Name')
    while True:
        age = input('Please Enter Employee Age: ')
        if age.isnumeric():
            break
        print('Incorrect Value Entered')
    while True:
        position = input('Please Enter Employee Position: ')
        if position.isalpha():
            break
        print('Invalid Position')
    while True:
        salary = input('Please Enter Employee Salary: £')
        if salary.isnumeric():
            break
        print('Incorrect Value Entered')
    while True:
        years = input('Please Enter Years Employed: ')
        if years.isnumeric():
            break
        print('Incorrect Value Entered')
    print('\nData Entered:')
    print(name, age, position, salary, years, sep=', ')
    return name, age, position, salary, years


with open('dummy.txt', 'a') as f:  # Automatically close a file at the end of usage
    for t in add_emp():  # Iterate over a tuple
        f.write(str(t) + " ") 
    f.write("\n")