Advertisement
jspill

webinar-python-input-patterns-2023-06-10

Jun 10th, 2023
1,274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. # Webinar Input Patterns June 10 2023
  2.  
  3. # input() ALWAYS returns a string
  4. # myInput = input() # that myInput var is definitely a str!
  5.  
  6. # but we might want to change it to something else...
  7.  
  8. # 1 Recast a numeric string into an int or float
  9. # 2 Breaking up a long string into a list of smaller strings
  10. # 3 Break up a string containing numeric chars into a list of
  11. #       recast ints or floats
  12. # 4 One value tells you how many times to call input()
  13. # 5 We DON'T KNOW how many times to call input(), but we know
  14. #       a sentinel value to stop
  15.  
  16.  
  17. # 1 Recast a numeric string into an int or float
  18. # looks a number...  5 so it's really "5"
  19. # myInput = int(input())
  20. # print(type(myInput))
  21.  
  22. # 2 Breaking up a long string into a list of smaller strings
  23. # "Pat Silly Doe"
  24. myInput = "Pat Silly Doe" # input()
  25. strList = myInput.split()
  26. # myInput = input().split()
  27. print(strList)
  28.  
  29. # getting a series of numbers... 12 44 67 23
  30. # 3 Break up a string containing numeric chars into a list of
  31. #       recast ints or floats
  32. myInput =  "31 333 2 78 92" # input()
  33. strList = myInput.split()
  34. print(strList)
  35. numList = []
  36. # for __ in __someContainer__
  37. for num in strList:
  38.     numList.append(int(num))
  39. print(numList)
  40.  
  41. # 4 One value tells you HOW MANY TIMES to call input()
  42. # 5 # so 5 more pieces of data coming in...
  43. # 30.0
  44. # 50.0
  45. # 10.0
  46. # 100.0
  47. # 65.0
  48.  
  49.  
  50. # numVals = int(input()) # get that first input
  51. # floatList = []
  52. #
  53. # for num in range(numVals):
  54. #     # get each next input, do your stuff
  55. #     nextInput = float(input())
  56. #     floatList.append(nextInput)
  57. # print(floatList)
  58.  
  59. # 5 We DON'T KNOW how many times to call input(), but we know to stop on some SENTINEL VALUE
  60. # this is a WHILE loop condition
  61.  
  62. # ask for the FIRST input()
  63. # myInput = input()
  64. # # THEN I can set up a while loop condition...
  65. # while myInput != "-1": # pay attention to string vs int
  66. #     print(f"I got this input: {myInput}")
  67. #     myInput = input()
  68. # print(f"I am done! I saw the sentinel value of {myInput}")
  69.  
  70. # Condition is never satisfied... comparing ints and strings
  71. # myInput = int(input())
  72. # while myInput != "-1":
  73. #     print(myInput * 2)
  74. #     myInput = int(input())
  75. # print(f"I am done! I saw the sentinel value of {myInput}")
  76.  
  77. # multiple quit commands
  78. # You can quit with "quit" or "done" or "d"
  79. myInput = input()
  80. quitCommands = ["quit", "done", "d"] # pro tip: put them in a list!
  81. while not myInput in quitCommands:
  82.     print(f"I received the command: {myInput}")
  83.     myInput = input()
  84. print("OK. Done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement