Advertisement
jspill

webinar-python-input-patterns-2023-11-11

Nov 11th, 2023
1,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. # Webinar Input Patterns 2023 Nov 11
  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.  
  15. # 1 Recast a numeric string into an int or float
  16. # 5 # that 5 is "5" but I might want the int 5
  17. # myInput = float(input())# int(input())
  18. # print(myInput)
  19. # print(type(myInput).__name__)
  20.  
  21. # 2 Breaking up a long string into a list of smaller strings
  22. # myInput = input()
  23. # myList = myInput.split()
  24. # print(myList)
  25.  
  26. # 12 28 34 # one input() to be split()
  27. # 12 # vs 3 separate calls to input()
  28. # 28
  29. # 34
  30.  
  31. # "Pat Silly Doe" or "Julia Clark"
  32. # myInput = input().split()
  33. # # myList = myInput.split()
  34. # print(myInput)
  35. # print(len(myInput))
  36.  
  37. # 3 Break up a string containing numeric chars into a list of
  38. #       recast ints or floats
  39. # 12 28 34 # one input() to be split()
  40. # myInput = input()
  41. # strList = myInput.split()
  42. # print(strList) # ['12', '99', '34', '25']
  43. #
  44. # numList = []
  45. # for item in strList:
  46. #     numList.append(int(item))
  47. # print(numList) # [12, 99, 34, 25]
  48.  
  49. # 4 One value tells you HOW MANY TIMES to call input()
  50. # Any "known number of times" means a for loop
  51. # 5
  52. # 30.0
  53. # 50.0
  54. # 10.0
  55. # 100.0
  56. # 65.0
  57.  
  58. # call input() to get the number of times
  59. # numVals = int(input())
  60. # floatList = []
  61. #
  62. # # loop with range()
  63. # for num in range(numVals):
  64. #     nextInput = float(input())
  65. #     floatList.append(nextInput)
  66. #     # floatList.append(float(input()))
  67. #
  68. # print(floatList)
  69.  
  70.  
  71. # 5 We DON'T KNOW how many times to call input(), but we know to stop on some SENTINEL VALUE
  72. # this is a WHILE loop condition
  73.  
  74. # myInput = input()
  75.  
  76. # set up a while loop
  77. # while myInput != "-1":
  78. #     # do whatever we need to do
  79. #     print(myInput)
  80. #     # get next input
  81. #     myInput = input()
  82. # print("OK. Quitting...")
  83.  
  84. # # Stop on quit or done or d
  85. quitCmds = ["quit", "done", "d"]
  86. myInput = input()
  87.  
  88. while not myInput in quitCmds:
  89.     # do stuff
  90.     print(f"I got the command {myInput}")
  91.     myInput = input()
  92. print("Done!")
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement