Created
December 4, 2012 18:29
-
-
Save skinofstars/4207221 to your computer and use it in GitHub Desktop.
Whitespace python for loop - for when you don't want to use split( )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
entry = input("Enter a Mayan date: ") | |
i=0 # this is used to track our position in the string | |
output = "" # start with output set to nothing | |
while entry[i:]: | |
# if we hit a space, then we've got to the end of that number | |
# print it out and reset the output | |
if entry[i] == " ": | |
print (output) | |
output = "" | |
# otherwise, append to the output string | |
else: | |
output += entry[i] | |
# increment position | |
i+=1 | |
# print the last number as there isn't a space after it | |
print(output) | |
############## | |
# easier way is to use split! | |
str = "3 52 9 54 67" | |
strList = str.split( ) # note the space between the brackets | |
print ('baktun ', strList[0]) | |
print ('katun ', strList[1]) | |
print ('tun ', strList[2]) | |
print ('uinal ', strList[3]) | |
print ('kin ', strList[4]) | |
############## | |
# or another way | |
# using a while loop for each part | |
entry = input("Enter a Mayan date: ") | |
baktun = "" | |
katun = "" | |
tun = "" | |
uinal = "" | |
kin = "" | |
i = 0 # our position tracker | |
# we pop out the loop when we find a space | |
while entry[i] != " ": | |
# print('debug baktun ', entry[i]) #uncomment these lines to see debug info. very useful | |
baktun += entry[i] | |
i += 1 | |
print (baktun) | |
i += 1 # moving along the array to make sure we're not on a space for the next comparison | |
# we still want to look for a space | |
while entry[i] != " ": | |
# print('debug katun ', entry[i]) | |
katun += entry[i] | |
i += 1 | |
print (katun) | |
i += 1 # make sure we keep moving along the array after every loop | |
while entry[i] != " ": | |
# print('debug tun ', entry[i]) | |
tun += entry[i] | |
i += 1 | |
print (tun) | |
i += 1 # still moving ;) | |
while entry[i] != " ": | |
# print('debug uinal ', entry[i]) | |
uinal += entry[i] | |
i += 1 | |
print (uinal) | |
i += 1 | |
# we're not looking for a space here, we just want whatever is remaining | |
# this is the only time we use the colon : | |
while entry[i:]: | |
# print('debug kin ', entry[i]) | |
kin += entry[i] | |
i += 1 | |
print (kin) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment