# Webinar Input Patterns Aug 12 2023 # input() ALWAYS returns a string # 11 # not a number, but a string if coming from input()! # 3 96 33 85 # 1 input, that we may want to split... vs the same "numbers" below # 3 # this is 4 calls to input() # 96 # 33 # 85 # myInput = input() # that myInput var is definitely a str! # but we might want to change it to something else... # Some common patterns... # 1 Recast a numeric string into an int or float # 2 Breaking up a long string into a list of smaller strings # 3 Break up a string containing numeric chars into a list of # recast ints or floats # 4 One value tells you how many times to call input() # 5 We DON'T KNOW how many times to call input(), but we know # a sentinel value to stop # 1 Recast a numeric string into an int or float # 5 looks like a number... 5 but it's really the string "5" # easy, recast as you call input() # myInput = int(input()) # float() # 2 Breaking up a long string into a list of smaller strings # That's the str split() method! # "Pat Silly Doe" or "Julia Clark" # myInput = input() # strList = myInput.split() # print(strList) # 3 Break up a string containing numeric chars into a list of # recast ints or floats # This combines the first 2 patterns above, and involves a for loop. # 12 44 67 23 # myInput = input() # strList = myInput.split() # # myInput = input().split() # print(strList) # numList = [] # for num in strList: # numList.append(int(num)) # print(numList) # 4 One value tells you HOW MANY TIMES to call input() # Any "known number of times" means a for loop # 5 # 30.0 # 50.0 # 10.0 # 100.0 # 65.0 # call input() to get the number of times # numVals = int(input()) # # for loop over range() # # floatList = [] # for num in range(numVals): # do this thing, this many times # nextInput = float(input()) # floatList.append(nextInput) # print(floatList) # 5 We DON'T KNOW how many times to call input(), but we know to stop on some SENTINEL VALUE # this is a WHILE loop condition # ask for the FIRST input() # myInput = input() # # then set up a WHILE LOOP # while myInput != "quit": # while myInput != "-1" # # do what you need with that input... # # # get the next input() # myInput = input() # get the next input for the next iteration of the loop # A variation on this: Multiple quit commands # quit, done, d myInput = input() quitCommands = ["quit", "done", "d"] while not myInput in quitCommands: # do your stuff print(f"I got the command: {myInput}") myInput = input() # again, get the next input print("OK. Done!")