Adnomination
Adnomination

Reputation: 153

Unable to add space in output

I have created a Python app to add Student and Student ID from console. It is working fine but the output is like "John01", where "John" is student name and "01" is student id. I am trying to get the result like "John 01".

Here is the function to save file:

def save_file(student, student_id):
    file = open("output.txt", "a")
    file.write(student + student_id + "\n")
    file.close()

I am new to Python and not sure how to add space between functions.

Here is my complete code:

students = []


def add_student(name, student_id):
    student = {"name": name, "student_id": student_id}
    students.append(student)


def save_file(student, student_id):
    file = open("output.txt", "a")
    file.write(student + student_id + "\n")
    file.close()


def user_input():
    student_name = input("Enter student name: ")
    student_id = input("Enter student id: ")
    add_student(student_name, student_id)
    save_file(student_name, student_id)
    add_more()


def add_more():
    add_request = input("Do you want to add more student information? (Y/N): ").lower().strip()
    if add_request[0] == "y":
        return user_input()
    else:
        print("Student added successfully")


user_input()

The code is working fine except the space issue. Here is the console output:

Enter student name: John
Enter student id: 01
Do you want to add more student information? (Y/N): y
Enter student name: Jane
Enter student id: 02
Do you want to add more student information? (Y/N): n
Student added successfully

Upvotes: 0

Views: 59

Answers (1)

Jinu p.r
Jinu p.r

Reputation: 26

use format function instead of +

file.write("{} {}\n".format(student, student_id))

Upvotes: 1

Related Questions