Input Patterns 2024 Aug 10
unknown
python
a month ago
2.0 kB
14
Indexable
Never
# Webinar Input Patterns 2024 Aug 10 # input() ALWAYS returns a string # 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 # myInput = int(input()) # print(type(myInput).__name__) # input().strip() # 2 Breaking up a long string into a list of smaller strings # "Pat Silly Doe" or "Julia Clark" # myInput = input().split() # # strList = myInput.split() # print(myInput) # 3 Break up a string containing numeric chars into a list of # recast ints or floats # 22 88 32 68 # myInput = input().split() # print(myInput) # # numList = [] # for num in myInput: # 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 # numTimes = int(input()) # # floatList = [] # # loop over that range() to get the next inputs # for n in range(numTimes): # range(0, numTimes, 1) # nextInput = float(input()) # floatList.append(nextInput) # # floatList.append(float(input()) # # 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 # get the first input() myInput = input() # set up a while loop using that var in its condition # while myInput != "quit": # while myInput != "-1": # # do your stuff with that piece of data # # myInput = input() # Use a list for multiple sentinel values # Stop on quit, or done, or d myInput = input() # put multiples in a list quitCommands = ["quit", "done", "d"] while not myInput in quitCommands: # do stuff myInput = input()
Leave a Comment