Input Patterns 2024 06 08
unknown
python
a year ago
2.3 kB
38
Indexable
# Webinar Input Patterns 2024 June 8 # input() ALWAYS returns a string # input() # 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 7 # not the int 7, but the string "7" # myInput = input() # myInput = float(input()) # or int(input()) # print(myInput) # 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) # strList = input().split() # print(strList) # 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 = [] # filtering pattern, "fill the basket" # for num in myInput: # # recast as int # # stick into new list # # myInt = int(num) # 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) # 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": not -1 but "-1", pay attention to type or you'll have an infinite loop! # # do your stuff # # # x += 1 # myInput = input() # Use a list for multiple sentinel values # Stop on quit, or done, or d myInput = input() quitCommands = ["quit", "done", "d"] while not myInput in quitCommands: # do your stuff myInput = input()
Editor is loading...
Leave a Comment