Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.23 KB | None | 0 0
  1.  
  2. """
  3. added a colon and space for neatness to the input string.
  4. Also renamed variables for clarity :)
  5. """
  6. user_digits = input('gimme some digits: ')
  7.  
  8. """
  9. In your version you started with "lists", then cast them to "sets" to remove duplicates.
  10. Very crafty use of a types properties!
  11. Since we don't want repeating values though we can use sets to start with.
  12. """
  13. odds = set()
  14. evens = set()
  15.  
  16. # you did great here!
  17. for letter in user_digits:
  18.     if int(letter) % 2 == 1:
  19.         odds.add(i) # bit different syntax because it's a set instead of a list
  20.     elif int(letter) % 2 == 0:
  21.         evens.add(letter) #same idea though
  22.  
  23. # Same idea as what you had, but duplicates are already gone and we do less casting
  24. odds = sorted(odds)
  25. evens = sorted(evens)
  26.  
  27. odds = ', '.join(odds)
  28. evens = ', '.join(evens)
  29.  
  30. """
  31. It's important to understand why these are evaluating to True and False.
  32. Python uses duck typing, which is useful, but also weird.
  33. "if not thing" will cast thing to a boolean - that is a True or False.
  34. empty sets/lists are cast as False, and ones with stuff are True
  35. """
  36. if not evens:
  37.     print('There are no even numbers in the input')
  38. else:
  39.     print('The even numbers in the input are: %s' %(evens))
  40. # this is perhaps a more explicit approach
  41. if len(odds) == 0:
  42.     print('There are no odd numbers in the input')
  43. else:
  44.     # another way of writing this, perhaps a bit more readable
  45.     # this is a newer feature in Python though
  46.     print(f'The odd numbers in the input are: {odds}')
  47.  
  48. """
  49. Okay - a little debrief. Upon review I'm realizing there's way too much technical jargon here.
  50. Sorry about that, genuinely. I get carried away.
  51. Python is a very high level language, so it does lots of stuff "under the hood" to make writing code simple.
  52. This can often be confusing for beginners as it obfuscates a lot of what's happening.
  53. But Python is still very beginner friendly and especially fun to make projects with.
  54.  
  55. Besides, your first goal as you're learning should be to "think like a programmer".
  56. That is working out logic puzzles and translating your ideas into executable code.
  57. The more institutional knowledge will come in due time.
  58.  
  59. Okay, that's enough for now, probably more then you bargained for. Keep it up!
  60. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement