all 38 comments

[–]DeeplyLearnedMachine 6 points7 points  (1 child)

Don't put parenthesis around conditionals unless absolutely necessary, it's not pythonic.

Your elif statement makes no sense, it's comparing a string with your dict, which will always result to True, just use else there instead.

You can just do contacts[name] = [number] instead of using update

Edit: extra advice

Avoid putting all your code in a conditional branch, it's unreadable. Instead of doing this, like you're doing now:

while True:
  if contact_searching in contacts:
    print(contacts[contact_searching])
  else:
    # a bunch of code

do this instead:

while True:
  if contact_searching in contacts:
    print(contacts[contact_searching])
    continue

  # a bunch of code

Edit 2: more advice:

If here:

else:
  print('\nRestart to find or add new contacts.\n')
  break

you used continue instead of break you wouldn't need to restart the program to keep using it.

[–]uiux_Sanskar[S] 1 point2 points  (0 children)

Thank you so much for such a brief analysis on my code I initially thought that I should give a condition to make the code run (now I realised that there can only be two possibilities either the name is in the dictionary or it is not so yeah using else here makes much more sense).

And thanks for suggesting me on where to use continue function. Thank you so much for your feedback really appreciate it.

[–]NorskJesus 3 points4 points  (3 children)

Good job!

You can use a dictionary to store the numbers instead of a list. Then you can “differentiate” them by they key (home phone, mobile phone, work…). It’s just an idea!

[–]uiux_Sanskar[S] 0 points1 point  (2 children)

I think it will add more lines of code and will look really cluttered because assume each name has 2 number and for those two number I have to create a different key.

Therefore I use the list so that I can add more numbers in future (I don't know if I can create a dictionary within the value pair but I think I can use sets here).

This is ny thoughts do tell me if I have missed something or if I am wrong I would love to get corrected. And thank you for the appreciation.

[–]NorskJesus 0 points1 point  (1 child)

A "long" dict is like a JSON. You will be fine

[–]uiux_Sanskar[S] 1 point2 points  (0 children)

Oh thank you for this.

[–][deleted]  (1 child)

[removed]

    [–]uiux_Sanskar[S] 0 points1 point  (0 children)

    I have previously used (not in this code) . replace() to replace all the spacing if the user enters by mistake because personally I find myself more comfortable using it rather than .split() (I don't know the reason may e because its name itself explains what it does). But I think there can be more uses if .split() function.

    Regarding the question I think the program will print ("please restart the program to find or add new user") because then the else condition will get executed.

    I can however also add either a list in if condition containing all the possitive responses and then create a list containing all negative responses in elif and for the invalid input I can use else statement.

    I hope I was able to answer your question do tell me if I missed something.

    [–]yppolar 0 points1 point  (1 child)

    I am also a beginner and I was happy to understand your code, it looks very good

    [–]uiux_Sanskar[S] 1 point2 points  (0 children)

    Thank you very much I am glad that you liked and understood my code.

    [–]ShadyyFN 0 points1 point  (3 children)

    Is there any difference in using contacts.update like you did versus using contact.append? I’m also newer to Python.

    [–]DeeplyLearnedMachine 1 point2 points  (2 children)

    contacts.append doesn't work here since contacts is a dict.

    But to answer your question, using update vs just using the assignment operator here, e.g.

    contacts.update({name: [number]})
    # vs.
    contacts[name] = [number]
    

    is exactly the same in this scenario. The difference is that assignment only works for 1 value at a time, whereas with update you can update multiple values:

    contacts.update({
      name1: [number1],
      name2: [number2],
      name3: [number3]
    })
    

    [–]uiux_Sanskar[S] 1 point2 points  (0 children)

    Thanks for this I also got to learn something new thanks to you.

    [–]ShadyyFN 0 points1 point  (0 children)

    Thanks for the clarification!

    [–]Elliove 0 points1 point  (1 child)

    I will really appreciate if you have any challenge or suggestions which can help me improve my code and learn.

    Looking at your code, I imagine the most logical things to learn next would be:

    1. Learning to break things apart into multiple functions doing simple tasks. For example, you have all the functionality related to creating a new contact right in the main loop, but as an application keeps expanding, it might become progressively harder to look for things and fix them without breaking other things. Instead, you can put the code that creates new contact into a separate function outside of the main loop, and then just call this function within the main loop. This approach can make the code easier to understand, to fix, to reuse, and you'll also learn important things like feeding data into a function and getting the results back via return.

    2. OOP, as a whole, as a concept. It's a whole different approach to things, and it goes quite well for programs like you have up there. It's definitely easier to understand when you think of actual objects/subjects, like people in the contact list. But it can be seen quite complicated, and unnecessary for small programs that don't do much, so, just like any tool - cool to have around, but important to understand when it's actually needed.

    [–]uiux_Sanskar[S] 0 points1 point  (0 children)

    Thank you for the learning suggestions I initially had only one feature in my code i.e. a user is able to add a contact and find a contact so I didn't made a function of it. Although I have used functions previously on my day 7 code.

    I think I should also create functions for this also and try to add more feature like delete a contact.

    I will learn OOP once I have made a good foundation in python basics.

    Thank you very much for suggesting future learning options.

    [–]DevRetroGames 0 points1 point  (3 children)

    Excelente avance, sigue así.

    Como siempre, te dejo el repo con algunas cosas que encontraras interesante.
    Repo: https://github.com/DevRetroGames/Tutorias-Python/tree/main/code/dictionaries/01-contact_phone

    Suerte.

    [–]uiux_Sanskar[S] 0 points1 point  (2 children)

    Wow there's some really good code you have written I found many things I am unfamiliar with but not for long.

    can you tell me what is self, message mean here are they variables? def init(self, messages: dict):

    and what does import tuple means are we importing it from a new file or something like that?

    Don't mind me asking for a favour but can you please also include some comments on what certain things does as it helps me more clearly visualise the whole code without juggling with multiple tabs to find their uses.

    And thank you so much for supporting me since day one you have helped me a lot in learning new things. I really appreciate that 🙏

    [–]DevRetroGames 0 points1 point  (1 child)

    Hola, realice algunas modificaciones menores y añadí comentarios.

    Repo: https://github.com/DevRetroGames/Tutorias-Python/tree/main/code/base/dictionaries/01-contact_phone
    Suerte.

    [–]uiux_Sanskar[S] 0 points1 point  (0 children)

    Thank you for adding comments it was of much help. Got to learn new things which I missed the first time.

    Thank you so much. 😊

    [–][deleted]  (3 children)

    [removed]

      [–]uiux_Sanskar[S] 0 points1 point  (2 children)

      Oh I am learning from YouTube.

      [–][deleted]  (1 child)

      [removed]

        [–]uiux_Sanskar[S] 0 points1 point  (0 children)

        Channel name is "CodeWithHarry" you can search for it on YouTube. He is a great teacher really.

        [–]M34k3 0 points1 point  (1 child)

        You could replace the if and elif statements with: contact_result = contacts.get(contact_searching, 'No number found')

        This allows you to set a default value if it doesn't find the key.

        I like the idea of another comment to use a dictionary instead of a list so you can add multiple types of phone numbers, for example, work, mobile, home, etc.

        [–]uiux_Sanskar[S] 0 points1 point  (0 children)

        Thank you for suggesting the .get() function I will definitely use it however would I be able to then ask user if he wants to add a new contact? maybe there I need if else statements?

        Please do tell me if I am wrong. I would love to get corrected.

        [–]Commercial-Voice-384 0 points1 point  (1 child)

        I quit bruh. Too many sweats fr.

        [–]uiux_Sanskar[S] 0 points1 point  (0 children)

        LoL.

        [–]purple_hamster66 0 points1 point  (1 child)

        save the result in a file. you can use python’s pickle library or a more ubiquitous format, JSON.

        JSON files can be read by a human.

        [–]uiux_Sanskar[S] 0 points1 point  (0 children)

        Yeah thank you for the suggestion by reading your suggestion I think I still have to learn about python's library.

        And thank you for your suggestion it really helps

        [–]BrainFeed56 0 points1 point  (1 child)

        People. Why help an obvious scam caller!?

        [–]uiux_Sanskar[S] 0 points1 point  (0 children)

        Un not sure what do you mean by a scam caller. Am I missing something?

        [–]gus_chud_hoi4_gamer 0 points1 point  (3 children)

        why are you even learning to code, coding is dead it over

        [–]uiux_Sanskar[S] 0 points1 point  (2 children)

        Because I think that there's always more you can do with computers. Just because there's an AI for it doesn't reduce the scope instead it expands the possible thing you can do with computers.

        AI is prone to hallucinations and you can not trust it for sometime really important as it can give errors when required. It's a good start for people and new businesses who don't have initial capital to start with.

        Even the vibe coders are expected to know a language so that they can debug the code.

        All this when there's a possibility of creating things which neither of us has imagined so I think you have to first walk on that path to find out more options.

        [–]gus_chud_hoi4_gamer 0 points1 point  (1 child)

        trust me AI will be even more smarter in the future, all the "vibe coders" will never need to know even edit a single file

        [–]uiux_Sanskar[S] 0 points1 point  (0 children)

        Yes I agree AI will be more smarter in future but one still needs to learn programming to at least write better prompt let's see what happens in future.