Advertisement
jspill

webinar-input-patterns-2024-02-10

Feb 10th, 2024
988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.07 KB | None | 0 0
  1. # Webinar Input Patterns 2024 Feb 10
  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. # 3 # not the int 3 it's "3"
  16. # myInput = int(input())
  17. # # myInput = int(myInput)
  18. # print(type(myInput).__name__)
  19.  
  20. # 2 Breaking up a long string into a list of smaller strings
  21. # "Pat Silly Doe" or "Julia Clark"
  22. # myInput = input().split()
  23. # print(myInput)
  24. # strList = myInput.split()
  25. # print(strList)
  26.  
  27. # 3 Break up a string containing numeric chars into a list of
  28. #       recast ints or floats
  29. # 24 88 32
  30. # myInput = input().split()
  31. # print(myInput)
  32. #
  33. # # fill the basket
  34. # numList = []
  35. # for num in myInput:
  36. #     numList.append(int(num))
  37. # print(numList)
  38.  
  39. # 4 One value tells you HOW MANY TIMES to call input()
  40. # Any "known number of times" means a for loop
  41. # 5
  42. # 30.0
  43. # 50.0
  44. # 10.0
  45. # 100.0
  46. # 65.0
  47.  
  48. # call input() to get the number of times
  49. # numTimes = int(input())
  50. # floatList = []
  51. #
  52. # # loop over that range() to get the next inputs
  53. # for n in range(numTimes):
  54. #     nextInput = float(input())
  55. #     floatList.append(nextInput)
  56. # print(floatList)
  57.  
  58. # 5 We DON'T KNOW how many times to call input(), but we know to stop on some SENTINEL VALUE
  59. # this is a WHILE loop condition
  60.  
  61. # get the first input()
  62. myInput = input()
  63.  
  64. # set up a while loop using that var in its condition
  65. # while myInput != "quit":
  66. # while myInput != -1:
  67. #     # do your stuff
  68. #
  69. #     myInput = input()
  70.  
  71. # Use a list for multiple sentinel values
  72. # Stop on quit, or done, or d
  73. myInput = input()
  74. quitCommands = ["quit", "done", "d"]
  75.  
  76. while not myInput in quitCommands: # this saves you from having more complex Boolean conditions
  77.     # do your stuff
  78.     myInput = input()
  79.  
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement