Python Convert String Literal to Float - python

I am working through the book "Introduction to Computation and Programming Using Python" by Dr. Guttag. I am working on the finger exercises for Chapter 3. I am stuck. It is section 3.2, page 25. The exercise is: Let s be a string that contains a sequence of decimal numbers separated by commas, e.g., s = '1.23,2.4,3.123'. Write a program that prints the sume of the numbers in s.
The previous example was:
total = 0
for c in '123456789':
total += int(c)
print total.
I've tried and tried but keep getting various errors. Here's my latest attempt.
total = 0
s = '1.23,2.4,3.123'
print s
float(s)
for c in s:
total += c
print c
print total
print 'The total should be ', 1.23+2.4+3.123
I get ValueError: invalid literal for float(): 1.23,2.4,3.123.

Floating point values cannot have a comma. You are passing 1.23,2.4,3.123 as it is to float function, which is not valid. First split the string based on comma,
s = "1.23,2.4,3.123"
print s.split(",") # ['1.23', '2.4', '3.123']
Then convert each and and every element of that list to float and add them together to get the result. To feel the power of Python, this particular problem can be solved in the following ways.
You can find the total, like this
s = "1.23,2.4,3.123"
total = sum(map(float, s.split(",")))
If the number of elements is going to be too large, you can use a generator expression, like this
total = sum(float(item) for item in s.split(","))
All these versions will produce the same result as
total, s = 0, "1.23,2.4,3.123"
for current_number in s.split(","):
total += float(current_number)

Since you are starting with Python, you could try this simple approach:
Use the split(c) function, where c is a delimiter. With this you will have a list numbers (in the code below). Then you can iterate over each element of that list, casting each number to a float (because elements of numbers are strings) and sum them:
numbers = s.split(',')
sum = 0
for e in numbers:
sum += float(e)
print sum
Output:
6.753

From the book Introduction to Computation and Programming using Python at page 25.
"Let s be a string that contains a sequence of decimal numbers separated by commas, e.g., s
= '1.23,2.4,3.123'. Write a program that prints the sum of the numbers in s."
If we use only what has been taught so far, then this code is one approach:
tmp = ''
num = 0
print('Enter a string of decimal numbers separated by comma:')
s = input('Enter the string: ')
for ch in s:
if ch != ',':
tmp = tmp + ch
elif ch == ',':
num = num + float(tmp)
tmp = ''
# Also include last float number in sum and show result
print('The sum of all numbers is:', num + float(tmp))

total = 0
s = '1.23,2.4,3.123'
for c in s.split(','):
total = total + float(c)
print(total)

Works Like A Charm
Only used what i have learned yet
s = raw_input('Enter a string that contains a sequence of decimal ' +
'numbers separated by commas, e.g. 1.23,2.4,3.123: ')
s = "," + s+ ","
total =0
for i in range(0,len(s)):
if s[i] == ",":
for j in range(1,(len(s)-i)):
if s[i+j] == ","
total = total + float(s[(i+1):(i+j)])
break
print total

This is what I came up with:
s = raw_input('Enter a sequence of decimal numbers separated by commas: ')
aux = ''
total = 0
for c in s:
aux = aux + c
if c == ',':
total = total + float(aux[0:len(aux)-1])
aux = ''
total = total + float(aux) ##Uses last value stored in aux
print 'The sum of the numbers entered is ', total

I think they've revised this textbook since this question was asked (and some of the other's have answered.) I have the second edition of the text and the split example is not on page 25. There's nothing prior to this lesson that shows you how to use split.
I wound up finding a different way of doing it using regular expressions. Here's my code:
# Intro to Python
# Chapter 3.2
# Finger Exercises
# Write a program that totals a sequence of decimal numbers
import re
total = 0 # initialize the running total
for s in re.findall(r'\d+\.\d+','1.23, 2.2, 5.4, 11.32, 18.1,22.1,19.0'):
total = total + float(s)
print(total)
I've never considered myself dense when it comes to learning new things, but I'm having a hard time with (most of) the finger exercises in this book so far.

s = input('Enter a sequence of decimal numbers separated by commas: ')
x = ''
sum = 0.0
for c in s:
if c != ',':
x = x + c
else:
sum = sum + float(x)
x = ''
sum = sum + float(x)
print(sum)
This is using just the ideas already covered in the book at this point. Basically it goes through each character in the original string, s, using string addition to add each one to the next to build a new string, x, until it encounters a comma, at which point it changes what it has as x to a float and adds it to the sum variable, which started at zero. It then resets x back to an empty string and repeats until all the characters in s have been covered

Here's a solution without using split:
s='1.23,2.4,3.123,5.45343'
pos=[0]
total=0
for i in range(0,len(s)):
if s[i]==',':
pos.append(len(s[0:i]))
pos.append(len(s))
for j in range(len(pos)-1):
if j==0:
num=float(s[pos[j]:pos[j+1]])
total=total+num
else:
num=float(s[pos[j]+1:pos[j+1]])
total=total+num
print total

My way works:
s = '1.23, 211.3'
total = 0
for x in s:
for i in x:
if i != ',' and i != ' ' and i != '.':
total = total + int(i)
print total

My answer is here:
s = '1.23,2.4,3.123'
sum = 0
is_int_part = True
n = 0
for c in s:
if c == '.':
is_int_part = False
elif c == ',':
if is_int_part == True:
total += sum
else:
total += sum/10.0**n
sum = 0
is_int_part = True
n = 0
else:
sum *= 10
sum += int(c)
if is_int_part == False:
n += 1
if is_int_part == True:
total += sum
else:
total += sum/10.0**n
print total

I have managed to answer the question with the knowledge gained up until 3.2 the section for loop
s = '1.0, 1.1, 1.2'
print 'List of decimal number'
print s
total = 0.0
for c in s:
if c == ',':
total += float(s[0:(s.index(','))])
d = int(s.index(','))+1
s = s[(d+1) : len(s)]
s = float(s)
total += s
print '1.0 + 1.1 + 1.2 = ', total
This is the answer to the question i feel that the split function is not good for beginner like you and me.

Considering the fact that you might not yet be exposed to more complex functions, simply try these out.
total = 0
for c in "1.23","2.4",3.123":
total += float(c)
print total

My answer:
s = '2.1,2.0'
countI = 0
countF = 0
totalS = 0
for num in s:
if num == ',' or (countF + 1 == len(s)):
totalS += float(s[countI:countF])
if countF < len(s):
countI = countF + 1
countF += 1
print(totalS) # 4.1
This only works if the numbers are floats

Here is my answer. It is similar to the one by user5716300 above, but since I am also a beginner I explicitly created a separate variable s1 for the split string:
s = "1.23,2.4,3.123"
s1 = s.split(",") #this creates a list of strings
count = 0.0
for i in s1:
count = count + float(i)
print(count)

If we are just sticking with the content for that chapter, I came up with this: (though using that sum method mentioned by theFourthEye is also pretty slick):
s = '1.23,3.4,4.5'
result = s.split(',')
result = list(map(float, result))
n = 0
add = 0
for a in result:
add = add + result[n]
n = n + 1
print(add)

I just wanna to post my answer because I am reading this book now.
s = '1.23,2.4,3.123'
ans = 0.0
i = 0
j = 0
for c in s:
if c == ',':
ans += float(s[i:j])
i = j + 1
j += 1
ans += float(s[i:j])
print(str(ans))

Using knowledge from the book:
s = '4.58,2.399,3.1456,7.655,9.343'
total = 0
index = 0
for string in s:
index += 1
if string == ',':
temp = float(s[:index-1])
s = s[index:]
index = 0
total += temp
temp = 0
print(total)
Here I used string slicing, and by slicing the original string every time our 'string' variable is equal to ','. Also using an index variable to keep track of the number that is before the comma. After slicing the string, the number that gets input into tmp is cleared with the comma in front of it, the string becoming another string without that number.
Because of this, the index variable needs to be reset every time this happens.

Here's mine using the exact string in the question and only what has been taught so far.
total = 0
temp_num = ''
for char in '1.23,2.4,3.123':
if char == ',':
total += float(temp_num)
temp_num = ''
else:
temp_num += char
total += float(temp_num) #to catch the last number that has no comma after it
print(total)

I know this isn't covered in the book up to this point but I happened to learn the use of the eval() function on my own prior to getting to this question and used it to solve.
total = 0
s = "1.23,2.4,3.123"
x = eval(s)
y = sum(x)
print(y)

I think this is the easiest way to answer the question. It uses the split command, which is not introduced in the book at this moment but a very useful command.
s = input('Insert string of decimals, e,g, 1.4,5.55,12.651:')
sList = s.split(',') #create a list of these values
print(sList) #to check if list is correctly created
total = 0 #for creating the variable
for each in sList:
total = total + float(each)
print(total)

total =0
s = {1.23,2.4,3.123}
for c in s:
total = total+float(c)
print(total)

Related

No iteration in nested while statement

How the loop should iterate. I'm a beginner trying to create a Python program to print a word backwards based on old knowledge from a few years ago. It does a few other things but they are just printed statements are don't pose a problem. Here is my code:
count = 0
while count < 100:
word_var = input("Enter ONE word with NO spaces:")
def split(word_var):
return list(word_var)
word_array = split(word_var)
m = 0
i = len(word_array)-1-m
print("The number of letters in your word is:", i)
while m < len(word_array):
if m < i:
word_array[m], word_array[i - m] = word_array[i - m], word_array[m]
m = m + 1
else:
break
m = m + 1
print(''.join(word_array))
count = count + 1
print("You've typed:",count,"word(s).")
Here is the problem section:
if m < i:
word_array[m], word_array[i - m] = word_array[i - m], word_array[m]
m = m + 1
else:
break
m = m + 1
My main problem is that it seems like the second while loop is not iterating when the word is more than five letters long. For example, if I input the word "should" into the program I get back out dhouls. It seems as if only one interchange of letters is being performed. I figure this is a problem with the if statement in that nested while loop, but I can't seem to find what is wrong with it. I carefully sketched out how I think the if statement works in the photo attached.
Your if condition is wrong. You want to compare the two indices that you will use in the list, but the second one is not i, but i-m. So change it to:
if m < i - m:
This corrects your issue. It should be noted that in Python you can reverse string just like this:
print(word_var[::-1])
There are two issues:
The counting of the letters isn't correct. You should just output the length of word_array.
You're iterating the while loop too many times. You should terminate it when m equals or exceeds len(word_array) // 2. Otherwise, you'll unreverse the letters and get the original word back.
i = len(word_array)-1
print("The number of letters in your word is:", len(word_array))
while m < len(word_array) // 2:
word_array[m], word_array[i - m] = word_array[i - m], word_array[m]
m = m + 1
This outputs:
Enter ONE word with NO spaces:should
The number of letters in your word is: 6
dluohs
You've typed: 1 word(s).
I like your project and appreciate your efforts.
This a another way to reverse a string using a list variable and the insert() method.
word_array = []
word_var = input('Your word : ')
word_array = []
for c in word_var:
word_array.insert(0, c)
word_reversed = ''.join(word_array)
print(word_var, '->', word_reversed)
output :
should -> dluohs

How to count specific substrings using slice notation

I want to count the number of occurrences of the substring "bob" within the string s. I do this exercise for an edX Course.
s = 'azcbobobegghakl'
counter = 0
numofiterations = len(s)
position = 0
#loop that goes through the string char by char
for iteration in range(numofiterations):
if s[position] == "b": # search pos. for starting point
if s[position+1:position+2] == "ob": # check if complete
counter += 1
position +=1
print("Number of times bob occurs is: " + str(counter))
However it seems that the s[position+1:position+2] statement is not working properly. How do i adress the two chars behind a "b"?
The second slice index isn't included. It means that s[position+1:position+2] is a single character at position position + 1, and this substring cannot be equal to ob. See a related answer. You need [:position + 3]:
s = 'azcbobobegghakl'
counter = 0
numofiterations = len(s)
position = 0
#loop that goes through the string char by char
for iteration in range(numofiterations - 2):
if s[position] == "b": # search pos. for starting point
if s[position+1:position+3] == "ob": # check if complete
counter += 1
position +=1
print("Number of times bob occurs is: " + str(counter))
# 2
You could use .find with an index:
s = 'azcbobobegghakl'
needle = 'bob'
idx = -1; cnt = 0
while True:
idx = s.find(needle, idx+1)
if idx >= 0:
cnt += 1
else:
break
print("{} was found {} times.".format(needle, cnt))
# bob was found 2 times.
Eric's answer explains perfectly why your approach didn't work (slicing in Python is end-exclusive), but let me propose another option:
s = 'azcbobobegghakl'
substrings = [s[i:] for i in range(0, len(s))]
filtered_s = filter(substrings, lambda s: s.startswith("bob"))
result = len(filtered_s)
or simply
s = 'azcbobobegghakl'
result = sum(1 for ss in [s[i:] for i in range(0, len(s))] if ss.startswith("bob"))

Programming error with string index out of range python

Every time I try to run my code and open a specific text document which is fairly long, it says string index out of range. I was wondering if any of you could help me solve this problem. I have to get my code to spit out more than what I have it doing right now, but this is a start and I didn't figure it wise to carry on without fixing the initial error.
file = open(input("Enter File Name:"))
lines = file.readlines()
file.close()
number_chars = 0
number_words = 0
largest_word = ""
smallest_word = ""
count_words = 0
largest = int(0)
smallest = int(999999)
average = 0
count = 0
sum = 0
for item in lines:
is_word = False
item = item.strip()
char = item[0]
count_words = count_words + 1
if char >= 'a' and char <= 'z':
is_word = True
elif char >= 'A' and char <= 'Z':
is_word = True
if is_word == True:
if(len(largest_word) < len(item)):
largest_word = item
if(len(smallest_word) > len(item)):
smallest_word = item
number_chars += len(item)
else:
item = int(item)
count = count + 1
sum = sum + item
if(count == 1 or item > largest):
largest = item
if(count == 1 or item < smallest):
smallest = item
print("Reading", "name_list.txt")
print("found",count,"numbers")
print("largest number :", largest)
print("smallest numer :", smallest)
Average = sum // count
print("Average :", Average)
print(largest_word)
print(smallest_word)
The first thing I'd be thinking about is the effect an empty line is going to have on the statements (primarily the last one):
file = open(input("Enter File Name:"))
lines = file.readlines()
for item in lines:
item = item.strip()
char = item[0]
An empty line will not have an index zero.
In terms of fixing it, it depends on your intent. From a casual glance, it appears that you treat each line as either a word or a number (though you may want to consider what happens if the line starts with :, for example - that won't be considered a word by your code and it's almost certainly going to cause problems when treating it as an integer).
However, putting that aside for now: since a blank line is neither a word nor a number, you may find the simplest fix is to simply ignore those blank lines totally, with something like:
item = item.strip() # This line already exists.
if item == "":
continue
A blank item will simply cycle around and get the next line.
However, as mentioned, this will only fix your immediate problem, you really should consider handling things that are not in the set {word, number}.

Calculating the totals of odd and even numbers from a file in Python

def main():
infile = open('numbers.txt','r')
evenTotal = 0
oddTotal = 0
line = infile.readline()
while line != '':
total += int(line)
line = infile.readline()
print('The total for the even numbers is',evenTotal)
print('The total for the odd numbers is',oddTotal)
infile.close()
print('All done!')
main()
I trying to make it so that the program reads the numbers from a file in its directory and then separates, calculates and displays the total of the two sets. The thing I am having trouble with is the part in the middle with the identification of the odds and the evens. I do know that the while loop I have written in the middle calculates the total but I don't know how to modify it to make it so it does what I want it to do.
from itertools you can use the partition recipe to partition into even and odd and return the sum of those
from itertools import ifilterfalse,imap,ifilter,tee
def partition(pred, iterable):
'Use a predicate to partition entries into false entries and true entries'
# partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
t1, t2 = tee(iterable)
return ifilterfalse(pred, t1), ifilter(pred, t2)
def is_odd(x):
return bool(x%2)
list_of_ints = imap(int,filter(lambda x:x.strip().isdigit(),infile))
odds, evens= partition(is_odd,list_of_ints)
print sum(evens),sum(odds)
it will likely be a little bit slower than freddies answer ...
but it is a good pattern to know
or as #JonClements pointed out in chat
r = range(11)
d = dict.fromkeys([0, 1], 0)
for i in r: d[i % 2] += i
is a neat way to do it
In order to check if a number is odd or even, you should use the modulus operator.
if an integer is evenly divisible by 2, it will be even, otherwise, it is odd.
So...
while line != '':
if int(line) % 2 == 0:
evenTotal += int(line)
else
oddTotal += int(line)
line = infile.readline()
use this for even number
def even_numbers(maximum):
return_string = ""
for x in range(2,maximum+1,2):
return_string += str(x) + " "
return return_string.strip()
# With this program in Python you will check a document for even and odd numbers and
# also it skips any text
# It also writes 2 extra files Oddfile and Evenfile
import re
fhand = open('numbers.txt') # file with numbers odd/even or even filled with text
text = fhand.read()
y = re.findall('[0-9]+', text)
sumeven = 0
sumodd = 0
Even = []
Odd = []
Oddfile=open('Oddfile.txt','w')
Evenfile=open('Evenfile.txt','w')
for number in y:
if (int(number) % 2) == 0:# Checks if the number is even
sumeven = sumeven + int(number)
Even.append(int(number))
Evenfile.write(str(number) + '\n')
if (int(number) % 2) == 1:# Checks if the number is odd
sumodd = sumodd + int(number)
Odd.append(int(number))
Oddfile.write(str(number) + '\n')
print("Even List is : ", Even)
print("Odd List is : ", Odd)

Python - Split integer into individual digits (undetermined amount of digits)

We are reading lines from a file which contain integers. There are an indeterminate amount of lines being read. We must split each integer into the digits of the integer and then add the digits together and create another file which writes in both the integer and the sum of the digits for each integer.
The professor said to use an event-controlled loop, but didn't specify beyond that. We are only permitted to use while loops, not for loops.
I can't figure out how to put code in here so that I can show what I have tested so far just to get reacquainted with the splitting of a certain amount of digits in an integer.
Edit to add:
myFile = open("cpFileIOhw_digitSum.txt", "r")
numbers = 1
numberOfLines = 3
digitList = []
while(numbers <= numberOfLines):
firstNum = int(myFile.readline())
while (firstNum != 0):
lastDigit = firstNum % 10
digitList.append(lastDigit)
firstNum = firstNum / 10
firstDigit = firstNum
digitList.append(firstDigit)
digitSum = sum(digitList)
print digitList
print digitSum
numbers += 1
myFile.close()
^Here is what I have so far but now my problem is I need each integer's digits stored in a different list. And this is an undetermined amount of integers being read from the file. The numbers used for the count and to end the loop are simply examples.
LASTEST UPDATE to my code: Pretty much all I need to know now is how to let the while loop know that there are no more lines left in the txt file.
myFile = open("cpFileIOhw_digitSum.txt", "r")
myNewFile = open("cpFileIOhw_output.txt", "w")
total = 0
fullInteger =
while(fullInteger != 0):
fullInteger = int(myFile.readline())
firstNum = fullInteger
while (firstNum != 0):
lastDigit = firstNum % 10
total = total + lastDigit
firstNum = firstNum / 10
firstDigit = firstNum
total = total + firstDigit
myNewFile.write(str(fullInteger) + "-" + str(total))
print " " + str(fullInteger) + "-" + str(total)
total = 0
myFile.close()
myNewFile.close()
Well, there are two ways to approach this:
cast the integer to a string and iterate through each character, casting each back to an int (best done with a for loop)
repeatedly get the dividend and modulo of dividing the integer by 10; repeat until the dividend is 0 (best done with a while loop).
And these are the math operators you need:
mod = 123 % 10 # 3
div = 123 // 10 # 12
Casting the integers to strings shouldn't be necessary. Since you're reading the integers in from a text file, you'll start with strings.
You'll need an outer while loop to handle reading the lines from the file. Since you're forbidden to use for loops, I would use my_file.readline(), and you'll know you're done reading the file when it returns an empty string.
Nested inside that loop, you'll need one to handle pulling apart the digits. Although-- did your professor require two loops? I thought that your question said that before it was edited, but now it doesn't. If it's not required, I would just use a list comprehension.
Try this for splitting into digits:
num = 123456
s = str(num)
summary = 0
counter = 0
while (counter < len(s)):
summary += int(s[counter])
counter += 1
print s + ', ' + str(summary)
Result:
C:\Users\Joe\Desktop>split.py
123456, 21
C:\Users\Joe\Desktop>
Try the following...
total = lambda s: str(sum(int(d) for d in s))
with open('u3.txt','r') as infile, open('u4.txt','w') as outfile:
line = '.' # anything other than '' will do
while line != '':
line = infile.readline()
number = line.strip('\n').strip()
if line != '':
outfile.write(number + ' ' + total(number) + '\n')
# or use ',' instead of ' 'to produce a CSV text file

Categories