Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Webinar Input Patterns 2024 Jan 13
- # 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
- # 7 # from input() that's not an integer, it's the string "7"
- # myInput = input()
- # myInput = float(myInput)
- # myInput = 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().split()
- # myString.split()
- # print(myInput)
- # print(type(myInput).__name__)
- # 3 Break up a string containing numeric chars into a list of
- # recast ints or floats
- # 12 28 34
- # myInput = input()
- # strList = myInput.split()
- # print(strList) # ['12', '28', '34', '88']
- #
- # numList = []
- # for num in strList:
- # numList.append(int(num))
- # # we can also do this as a LIST COMPREHENSION
- # # expression for thing in container
- # # numList = [int(num) for num in strList]
- # 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())
- # floatList = []
- #
- # # loop over that range() to get the next inputs
- # for n in range(numVals):
- # 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 != "-1":
- # # do whatever we need with that input
- # myInput = input()
- # Use a list for multiple sentinel values
- # Stop on quit, or done, or d
- myInput = input()
- quitCommands = ["quit", "done", "d"]
- # you could use a Boolean combined condition here but it's simpler to create a list and check membership in it
- while not myInput in quitCommands:
- # do your stuff
- myInput = input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement