Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- added a colon and space for neatness to the input string.
- Also renamed variables for clarity :)
- """
- user_digits = input('gimme some digits: ')
- """
- In your version you started with "lists", then cast them to "sets" to remove duplicates.
- Very crafty use of a types properties!
- Since we don't want repeating values though we can use sets to start with.
- """
- odds = set()
- evens = set()
- # you did great here!
- for letter in user_digits:
- if int(letter) % 2 == 1:
- odds.add(i) # bit different syntax because it's a set instead of a list
- elif int(letter) % 2 == 0:
- evens.add(letter) #same idea though
- # Same idea as what you had, but duplicates are already gone and we do less casting
- odds = sorted(odds)
- evens = sorted(evens)
- odds = ', '.join(odds)
- evens = ', '.join(evens)
- """
- It's important to understand why these are evaluating to True and False.
- Python uses duck typing, which is useful, but also weird.
- "if not thing" will cast thing to a boolean - that is a True or False.
- empty sets/lists are cast as False, and ones with stuff are True
- """
- if not evens:
- print('There are no even numbers in the input')
- else:
- print('The even numbers in the input are: %s' %(evens))
- # this is perhaps a more explicit approach
- if len(odds) == 0:
- print('There are no odd numbers in the input')
- else:
- # another way of writing this, perhaps a bit more readable
- # this is a newer feature in Python though
- print(f'The odd numbers in the input are: {odds}')
- """
- Okay - a little debrief. Upon review I'm realizing there's way too much technical jargon here.
- Sorry about that, genuinely. I get carried away.
- Python is a very high level language, so it does lots of stuff "under the hood" to make writing code simple.
- This can often be confusing for beginners as it obfuscates a lot of what's happening.
- But Python is still very beginner friendly and especially fun to make projects with.
- Besides, your first goal as you're learning should be to "think like a programmer".
- That is working out logic puzzles and translating your ideas into executable code.
- The more institutional knowledge will come in due time.
- Okay, that's enough for now, probably more then you bargained for. Keep it up!
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement