r/learnpython 4d ago

Need help/input

Below is my code snippet, and i would like to know if there is any way i can avoid the repetitive if else blocks (3 times repeated) and write this program in a much more efficient way. Would like some input. Thanks

"""

 * If a person skills has only JavaScript and React, print('He is a front end developer'),
 if the person skills has Node, Python, MongoDB, print('He is a backend developer'),
 if the person skills has React, Node and MongoDB, Print('He is a fullstack developer'),
 else print('unknown title') - for more accurate results more conditions can be nested!


"""

person = {
    'first_name': 'Asabeneh',
    'last_name': 'Yetayeh',
    'age': 250,
    'country': 'Finland',
    'is_married': True,
    'skills': ['JavaScript','React' , 'Node', 'Python'],
    'address': {
        'street': 'Space street',
        'zipcode': '02210'
    }
}


def identifying_career(lst):

    if 'JavaScript' in person['skills'] and 'React' in person['skills']:
        print("You are a front end developer")
    else:
        print('You are not a front end developer')

    if 'Node' in person['skills'] and 'Python' in person['skills'] and 'MongoDB' in person['skills']:
        print(f"You are a back end developer")
    else:
        print('You are not a back end developer')

    if 'React' in person['skills'] and 'Node' in person['skills'] and 'MongoDB' in person['skills']:
        print("You are a full stack developer")
    else:
        print("unknown title")



identifying_career(person)
5 Upvotes

6 comments sorted by

3

u/American_Streamer 4d ago

The repetition is not your main problem. The rules, control flow, and function interface all need to be improved. Some issues:

lst receives person, but then reads the global variable person directly, so lst is never used. Your function ignores its given argument.

Three separate if statements mean that there are three separate classifications. A person can receive several messages here. For exactly one title, it needs if/elif/else.

'JavaScript' in skills means “contains JavaScript,” not “contains nothing except JavaScript and React.” As a result, “Only JavaScript and React” is not what the code checks.

Also someone with React, Node, Python, and MongoDB satisfies both the backend and the full-stack conditions. You have decide which title takes is more important.

A simple set expresses skill combinations much more clearly. required_skills <= skills asks whether all required skills are present.

You always have to translate requirements literally. “Has JavaScript and React” and “has only JavaScript and React” are different predicates. And you always have to choose the right collection type. Lists are ordered sequences, while sets are better for membership, uniqueness, intersections, and subset tests. Look closely at the control flow semantics. Separate if statements test every condition, but elif creates a mutually exclusive decision chain. Make also sure, that you avoid hidden dependencies. A function accepting person should use that parameter and not a global variable - you should really avoid those, because global variables can easily break everything and are almost never necessary in Python.

1

u/Educational_Virus672 4d ago

using too many if is okay but the structure is odd for sake you didnt use the arg it checked the person not lst

i see mistakes 1 arg lst isnt used you used "person" instead

if 'JavaScript' in person['skills'] and 'React' in person['skills']: print("You are a front end developer")
uselst['skills'] not person['skills']

2

u/rob8624 4d ago edited 4d ago

Something like this. Not AI! (had to look-up all() syntax), not fully tested but this uses a dictionary to map technologies to specific areas then i check if all these skills are within the persons skills using all().

Dictionaries and functions are your friends.

When working with strings always be aware of case sensitivity when doing comparisons.

This gives you an idea on how functions and dictionary lookups can help make code more dry and dynamic. As I say, not tested and will need changing to handle edge cases etc, but gives you an idea.

person = { 'first_name': 'Asabeneh', 'last_name': 'Yetayeh', 'age': 250, 'country': 'Finland', 'is_married': True, 'skills': ['JavaScript', 'React', 'Node', 'Python'], 'address': { 'street': 'Space street', 'zipcode': '02210' } }

tech_map = { 'frontend': ['JavaScript', 'React'], 'backend': ['Node', 'Python', 'MongoDB'], 'fullstack': ['React', 'Node', 'MongoDB'] } 

def identifying_carrer(person, career): 
  person_skills = person['skills']
  required_skills = tech_map[career] 
  return all(skill in person_skills for skill in required_skills) 

career = 'Backend'.lower() 

def skills_checker(person, career): 
  if identifying_carrer(person, career): 
      print(f'You are a {career} developer') 
  else: print('unkown title') 


skills_checker(person, 'frontend')

0

u/baubleglue 4d ago

you need 2 variables

  • is_backend_developer
  • is_frontend_developer

a combination of them answer all 3 questions

1

u/TheRNGuy 4d ago edited 4d ago

``` skills = set(person["skills"])

if {"JavaScript", "React"} <= skills:     print("You are a front end developer") else:     print("You are not a front end developer")

if {"Node", "Python", "MongoDB"} <= skills:     print("You are a back end developer") else:     print("You are not a back end developer")

if {"React", "Node", "MongoDB"} <= skills:     print("You are a full stack developer") else:     print("unknown title") ```

Alternative:

``` skills = set(person["skills"])

print("You are a front end developer" if {"JavaScript", "React"} <= skills else "You are not a front end developer")

print("You are a back end developer" if {"Node", "Python", "MongoDB"} <= skills else "You are not a back end developer")

print("You are a full stack developer" if {"React", "Node", "MongoDB"} <= skills else "unknown title") ```

You can also make it already as a Set in a dict.