Advertisement
jspill

webinar-python-input-patterns-2023-08-12

Aug 12th, 2023
1,612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. # Webinar Input Patterns Aug 12 2023
  2.  
  3. # input() ALWAYS returns a string
  4.  
  5. # 11 # not a number, but a string if coming from input()!
  6. # 3 96 33 85 # 1 input, that we may want to split... vs the same "numbers" below
  7. # 3 # this is 4 calls to input()
  8. # 96
  9. # 33
  10. # 85
  11.  
  12. # myInput = input()  # that myInput var is definitely a str!
  13. # but we might want to change it to something else...
  14.  
  15. # Some common patterns...
  16. # 1 Recast a numeric string into an int or float
  17. # 2 Breaking up a long string into a list of smaller strings
  18. # 3 Break up a string containing numeric chars into a list of
  19. #       recast ints or floats
  20. # 4 One value tells you how many times to call input()
  21. # 5 We DON'T KNOW how many times to call input(), but we know
  22. #       a sentinel value to stop
  23.  
  24.  
  25. # 1 Recast a numeric string into an int or float
  26. # 5 looks like a number...  5 but it's really the string "5"
  27. # easy, recast as you call input()
  28. # myInput = int(input()) # float()
  29.  
  30. # 2 Breaking up a long string into a list of smaller strings
  31. # That's the str split() method!
  32. # "Pat Silly Doe" or "Julia Clark"
  33. # myInput = input()
  34. # strList = myInput.split()
  35. # print(strList)
  36.  
  37. # 3 Break up a string containing numeric chars into a list of
  38. #       recast ints or floats
  39. # This combines the first 2 patterns above, and involves a for loop.
  40. # 12 44 67 23
  41. # myInput = input()
  42. # strList = myInput.split()
  43. # # myInput = input().split()
  44. # print(strList)
  45. # numList = []
  46. # for num in strList:
  47. #     numList.append(int(num))
  48. # print(numList)
  49.  
  50. # 4 One value tells you HOW MANY TIMES to call input()
  51. # Any "known number of times" means a for loop
  52. # 5
  53. # 30.0
  54. # 50.0
  55. # 10.0
  56. # 100.0
  57. # 65.0
  58.  
  59. # call input() to get the number of times
  60. # numVals = int(input())
  61. # # for loop over range()
  62. #
  63. # floatList = []
  64. # for num in range(numVals): # do this thing, this many times
  65. #     nextInput = float(input())
  66. #     floatList.append(nextInput)
  67. # print(floatList)
  68.  
  69.  
  70. # 5 We DON'T KNOW how many times to call input(), but we know to stop on some SENTINEL VALUE
  71. # this is a WHILE loop condition
  72.  
  73. # ask for the FIRST input()
  74. # myInput = input()
  75. # # then set up a WHILE LOOP
  76. # while myInput != "quit": # while myInput != "-1"
  77. #     # do what you need with that input...
  78. #
  79. #     # get the next input()
  80. #     myInput = input() # get the next input for the next iteration of the loop
  81.  
  82. # A variation on this: Multiple quit commands
  83. # quit, done, d
  84. myInput = input()
  85. quitCommands = ["quit", "done", "d"]
  86. while not myInput in quitCommands:
  87.     # do your stuff
  88.     print(f"I got the command: {myInput}")
  89.     myInput = input() # again, get the next input
  90. print("OK. Done!")
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement