Discuss Confusion on txt files, lists and for loops in the Python Programming forum on Dev Shed. Confusion on txt files, lists and for loops Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.
I made a program that opens the file, reads the first line of text, then checks to see if it is a valid number. Here is the code for that:
Code:
def readfile():
'''Read a text file; return a string holding the text'''
f = open("numbers.txt", 'r')
text = f.readline()
f.close()
return text
def dataConversion(text):
# create a list called lst and use getLine as input for the list
lst = text.strip()
# convert strings into ints
lst = map(int, lst)
#return lst to main() function
return lst
def evenNumberList(lst):
#strip last digit from lst
lst.pop()
# make a new list with every other number from right to left
evenNumber =[lst[i] for i in range(len(lst)-1,-1,-2)]
# create empty list
doubledList = []
# loop through the list
for num in evenNumber:
#double each number in the list
value = num *2
#append new value to doubledList list
doubledList.append(value)
return doubledList
def dataAddition(doubledList):
# make new list consisting of the single digit numbers of the
# doubledList
listOfSingleDigits = [x for x in doubledList if x <= 9]
# make new list consisting of the double digit numbers of the
# doubledList. Split the double digits into single numbers
# and add them together to get single digits.
listOfDoubleDigits = [x % 10 + x / 10 for x in doubledList if x > 9]
# create add fuction for reduce statement
def add(x, y): return x + y
# add the values of the listOfSingleDigits list
sumOfList1 = reduce(add, listOfSingleDigits)
# add the values of the listOfDoubleDigits list
sumOfList2 = reduce(add, listOfDoubleDigits)
#add sumOfList1, sumOfList2, sumOfList3
sumOfAllSingleDigits = sumOfList1 + sumOfList2
return sumOfAllSingleDigits
def oddNumberList(lst):
# make a new list with every other number from right to left
oddNumber =[lst[i] for i in range(len(lst)-1,-1,-2)]
#create add function for reduce statement
def add(x, y): return x + y
sumOfOddNumbers = reduce(add, oddNumber)
return sumOfOddNumbers
def checkCardValidity(sumOfAllSingleDigits, sumOfOddNumbers, text):
# read one line of text from the text file
creditCardNumber = text
#add numbers for validation
sumOfEvenAndOdd = sumOfAllSingleDigits + sumOfOddNumbers
#create if statement to check card validity
if sumOfEvenAndOdd / 10 == 0:
print creditCardNumber, "valid"
else:
print creditCardNumber, "invalid"
def main():
text = readfile()
lst = dataConversion(text)
doubledList = evenNumberList(lst)
sumOfAllSingleDigits = dataAddition(doubledList)
sumOfOdd = oddNumberList(lst)
sumOfOddNumbers = oddNumberList(lst)
checkCardValidity(sumOfAllSingleDigits, sumOfOddNumbers, text)
main()
Now that I have that working, I am trying to go back and get it to loop through each line of numbers. Here is where I am having problems. Or I think I am. I assumed that when the text file went through the loop it would end up looking like this:
Then from there I could split each large string up into individual character strings within the list. However, it's not working out as I planned. Instead I get the following:
It's just one giant list of individual character strings.
How do I get each number into it's own list so that manipulate each list and make it work with the rest of my program like the single number did?
Here is what I tried:
Code:
def dataConversion(text):
lst = []
for line in text:
numberList = line.strip()
lst.append(numberList)
print lst
I tried to go beyond that by converting the single character strings into int data types but it it gives me the following error message: ValueError: invalid literal for int() with base 10: ''