Advertisement
jspill

webinar-python-input-patterns-2024-01-13

Jan 13th, 2024
1,247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. # Webinar Input Patterns 2024 Jan 13
  2.  
  3. # input() ALWAYS returns a string
  4.  
  5. # Some common patterns...
  6. # 1 Recast a numeric string into an int or float
  7. # 2 Breaking up a long string into a list of smaller strings
  8. # 3 Break up a string containing numeric chars into a list of
  9. #       recast ints or floats
  10. # 4 One value tells you how many times to call input()
  11. # 5 We DON'T KNOW how many times to call input(), but we know
  12. #       a sentinel value to stop
  13.  
  14. # 1 Recast a numeric string into an int or float
  15. # 7  # from input() that's not an integer, it's the string "7"
  16. # myInput = input()
  17. # myInput = float(myInput)
  18. # myInput = int(input())
  19. # print(myInput)
  20. # print(type(myInput).__name__)
  21.  
  22. # 2 Breaking up a long string into a list of smaller strings
  23. # "Pat Silly Doe" or "Julia Clark"
  24. # myInput = input().split()
  25. # myString.split()
  26. # print(myInput)
  27. # print(type(myInput).__name__)
  28.  
  29. # 3 Break up a string containing numeric chars into a list of
  30. #       recast ints or floats
  31. # 12 28 34
  32. # myInput = input()
  33. # strList = myInput.split()
  34. # print(strList) # ['12', '28', '34', '88']
  35. #
  36. # numList = []
  37. # for num in strList:
  38. #     numList.append(int(num))
  39. # # we can also do this as a LIST COMPREHENSION
  40. # # expression for thing in container
  41. # # numList = [int(num) for num in strList]
  42. # print(numList)
  43.  
  44. # 4 One value tells you HOW MANY TIMES to call input()
  45. # Any "known number of times" means a for loop
  46. # 5
  47. # 30.0
  48. # 50.0
  49. # 10.0
  50. # 100.0
  51. # 65.0
  52.  
  53. # call input() to get the number of times
  54. # numVals = int(input())
  55. # floatList = []
  56. #
  57. # # loop over that range() to get the next inputs
  58. # for n in range(numVals):
  59. #     nextInput = float(input())
  60. #     floatList.append(nextInput)
  61. # print(floatList)
  62.  
  63. # 5 We DON'T KNOW how many times to call input(), but we know to stop on some SENTINEL VALUE
  64. # this is a WHILE loop condition
  65.  
  66. # get the first input()
  67. # myInput = input()
  68. # set up a while loop using that var in its condition
  69. # while myInput != "-1":
  70. #     # do whatever we need with that input
  71. #     myInput = input()
  72.  
  73. # Use a list for multiple sentinel values
  74. # Stop on quit, or done, or d
  75. myInput = input()
  76. quitCommands = ["quit", "done", "d"]
  77.  
  78. # you could use a Boolean combined condition here but it's simpler to create a list and check membership in it
  79. while not myInput in quitCommands:
  80.     # do your stuff
  81.     myInput = input()
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement