[Python] How to copy dictionary values to a class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

11th Jul 2019, 6:09 PM
The MA
7 Answers
+ 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.
23rd Jul 2019, 12:10 AM
NulledNVoided
NulledNVoided - avatar
+ 1
Thank you NulledNVoided for your answer
23rd Jul 2019, 3:05 AM
The MA
+ 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!
23rd Jul 2019, 3:06 AM
NulledNVoided
NulledNVoided - avatar
+ 1
That's cheerful, definitely I will. After couple of days I thought my question won't be answered and lost hope. Many thanks
23rd Jul 2019, 3:11 AM
The MA
+ 1
You're welcome bud. I like to go through the unanswered for that reason
23rd Jul 2019, 3:13 AM
NulledNVoided
NulledNVoided - avatar
+ 1
Wish many people do what you're doing
23rd Jul 2019, 3:31 AM
The MA
+ 1
Honestly first time I logged on in a while.
23rd Jul 2019, 4:00 AM
NulledNVoided
NulledNVoided - avatar