Example code - Lists
		
		
		
		Jump to navigation
		Jump to search
		
Analysing string content
This is an upgrade of the previous "Determine the input". Here every word, integer and float on the line is counted. Quite a few list concepts are used - on purpose.
inputstring = input("Enter something and I will figure out what it is: ")
inlist = inputstring.split()
# ints, floats and words count
resultlist = [0, 0, 0]
resultname = ('ints', 'floats', 'words')
# Analyze
for item in inlist:
    try:
        itempos = 2
        result = float(item)
        itempos = 1
        result = int(item)
        itempos = 0
    except ValueError:
        pass
    resultlist[itempos] += 1
printlist = list()
if inputstring == "":
    printlist.append("No input")
elif len(inlist) == 0:
    printlist.append("Just some whitespace")
else:
    # Remove non-interesting 0 results - no need to say you found 0 integers.
    for i in range(len(resultlist)):
        if resultlist[i] != 0:
            printlist.append(str(resultlist[i]) + " " + resultname[i])
print("The input had:", ' and '.join(printlist))