Webinar Input Patterns Sept 2024
unknown
python
a month ago
2.1 kB
19
Indexable
Never
# Webinar Input Patterns Sept 14 # 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 = input() # print(type(myInput).__name__) # myInput = int(input()) # print(type(myInput).__name__) # 2 Breaking up a long string into a list of smaller strings # "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 # 22 88 32 68 # strList = input().split() # print(strList) # # # fill the basket # numList = [] # for num in strList: # recast = int(num) # numList.append(recast) # or # 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()) # # # fill the basket # floatList = [] # for n in range(0, numTimes): # 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 # get the first input() # myInput = input() # set up a while loop using that var in its condition # while myInput != "quit": # while myInput != "-1": # while myInput != -1: # # do the stuff you need with that piece of data # # myInput = input() # the next for next iteration # # Variation on this pattern: multiple sentinel values! # Use a list for multiple sentinel values # Stop on quit, done, or d # myInput = input() # quitCommands = ["quit", "done", "d"] # # while not myInput in quitCommands: # # do stuff # myInput = input()
Leave a Comment