Apr-10-2021, 04:56 PM
I'm a new python learner and I've been looking at several different sources to understand how python works. Today I decided to challenge myself by making a problem which stores user input on several aspects of a desired phone on a text file, using the concept of classes. However, I'm slightly annoyed at how the information is presented on the text file. At the moment it presents the information in one straight line like so:
My code:
Brand: Samsung.Model: Samsung Galaxy A40.Year Produced: 2019.Information Provided By JeffreyHowever, I want it to be presented like so:
Brand: Samsung. Model: Samsung Galaxy A40 Year Produced: 2019 Information Provided By JeffreyAny suggestions?
My code:
#This program recieves phone data input from the user, and stores it in an array (within the class). It then adds it to #a text file. class phoneData: def __init__(self, brand, modelName, yearProduced): self.brand = brand self.modelName = modelName self.yearProduced = yearProduced def printstoredResult(self): print("The " + self.modelName + " was made in " + self.yearProduced + " by " + self.brand) try: file = open("phoneData.txt") except: print("No such file exists") file = open("phoneData.txt", "x") file.close() finally: name = input("Please Enter Your Name") print("Hello " + name + "! Welcome To The PDIS (Phone Data Input Service). We store information about phones in our data base for reference!") userInput = input("Would you like to input some phone information? Answer with yes or no.") filteredInput = userInput.replace(" ", "") if filteredInput == "yes" or filteredInput == "Yes": brand = input("Alright. Please enter the desired phone brand.") newBrand = brand.replace(" ", "") modelName = input("That's cool! A Phone Made By " + newBrand + "! Enter the model name now!") yearProduced = input("Finally, enter the year it was produced in.") filteredYear = yearProduced.replace(" ", "") print("That's great!") newPhone = phoneData(newBrand, modelName, filteredYear) newPhone.printstoredResult() file = open("phoneData.txt", "a") file.write("Brand: " + newPhone.brand + "." "Model: " + newPhone.modelName + "." "Year Produced: " + newPhone.yearProduced + "." "Information Provided By " + name) else: print("Oh ok! Goodbye!")