+1

[Python] How to copy dictionary values to a class?

I wrote below code and i'm not sure how to fill the class with the dictionary values. From FileName import Phone mBrand= "" mModel="" mScreen="" mBattery="" mobile = {"brand": mBrand, "model": mModel, "screen size": mScreen, "battery capacity": mBattery} For i in mobile: mobile[i] = input("Please enter your mobile" +i) while i == sorted(mobile.values())[0]: print("Your mobile "+i+ " is " +mobile[i]) """this code does not work Phone(i) = mobile[i] """ So i confirmed everything is working fine till this step but after that how can i cast the values of mobile dictionary to below class which is on another file class Phone(): def __init__(self, brand, model, screen, size): self.brand = brand self.model = model self.screen = screen self.size = size

7/11/2019 6:09:47 PM

Me Only

7 Answers

New Answer

+1

Hey Me Only. One thing that could be causing the issue is that a class gets saved as a variable in Python so you save it as VarName = Phone(a,b,c,..,z). the way you could save them is with the adding a second dictionary in and saving the values under the value from the previous dictionary. Example NewDictName = {mobile["model"]: Phone(...)} or add another spot for the user to enter the name in the previous dictionary and save it like that instead and then save the models or names or whatever you choose in a list or anything you like and that should work. However, I can't test it rn.

+1

Thank you NulledNVoided for your answer

+1

You're welcome bud! I hope that it works for you. If you need anything else please feel free to message me. Happy coding!

+1

That's cheerful, definitely I will. After couple of days I thought my question won't be answered and lost hope. Many thanks

+1

You're welcome bud. I like to go through the unanswered for that reason

+1

Wish many people do what you're doing

+1

Honestly first time I logged on in a while.