Python - Reading File Then Adding Value to Sum? - python-3.x

I've been looking all around the web for this answer and can't find it.
I'm semi new to python so hoping i get an answer here,
so basically what i want to do is, access a text file "number.txt"
that has a 10 as a line, and do a sum within the python code.
Here is what i got so far:
with open('number.txt', 'r') as sum:
num = sum.readline()
clean = num.rstrip('\n')
#number.txt file only contains 1 line and is a 10
increase = "5"
adding = clean + increase
print(adding)
it doesn't do the sum, instead i get the 5 added after the 10
so instead of getting 15 i get 105.
can anyone help?

Welcome to programming. Keep aware of the concepts of data types, I suggest doing some reading on that.
The + operator behaves differently between strings, arrays, and integers.
I would give you the answer but I am guess you are doing this as an assignment of sorts, so I just wanted to point you in the right direction. Use a python debugger (like PyCharm, or Wing IDE) to determine the differences between "5" + "10" 5+10 and [5]+[10]
You need to look into what's called Type Casting

Related

Python 3 Tips to Shorten Code for Assignment and Getting Around TextIO

I've been going through a course and trying to find ways to shorten my code. I had this assignment to open a text file, split it, then add all of the unique values to a list, then finally sort it. I passed the assignment, but I have been trying to shorten it to learn some ways to apply any shortening concepts to future codes. The main issues I keep running into is trying to make the opened file into strings to turn them into lists to append and such without read(). If I don't used read() I get back TextIO errors. I tried looking into it but what I found involved importing os and doing some other funky stuff, which seems like it would take more time.
So if anyone would mind giving me tips to more effectively code this that are beginner friendly I would be appreciative.
romeo = open('romeo').read()
mylist = list()
for line in romeo.split() :
if line not in mylist:
mylist.append(line)
mylist.sort()
print(mylist)
I saw that set() is pretty good for unique values, but then I don't think I can sort it. Then trying flip flop between a list and set would seem wacky. I tried those swanky one line for loop boys, but couldn't get it to work. like for line not in mylist : mylist.append(line) I know that's not how to do it or even close, but I don't know how to convey what I mean.
So to iterate:
1. How to get the same result without read() / getting around textIO
2. How to write this code in a more stream lined way.
I'm new to the site and coding, so hopefully I didn't trigger anyone.

Print a variable with index (eg. x_1)

i am trying to print variables with indices.
The goal is to write something like:
x_1 + x_2 = 3 + 1 = 4
The problem is, that variables just like x_1 does not show an index. It shows the underscore itself.
/* works fine */
print(x_1)$
x_1;
/* Does not work */
ostream: make_string_output_stream()$
printf(ostream, string(x_1))$
get_output_stream_string(ostream);
Output of the code above
Converting "x_1" into a string destroys the underscore, but you need a string for the method printf().
Are there workarounds for this?
What to do here probably depends somewhat on what goal you need to achieve with this. Can you say more about the larger goal you are working toward?
In order to generate output which has typesetting instructions in it, you can call the tex or tex1 function to generate TeX output. If that needs to be displayed in a web page, I believe you can make use of the MathJax Javascript library. Sorry, I don't know more about MathJax.
You should probably write x[1] instead of x_1. Displaying x_1 with a subscript 1 is a convenience -- the rest of Maxima doesn't know much about it.
EDIT: There is also an add-on package to output MathML; there might be two such packages, I would have to check. If MathML could help solve your problem, I will look into it.

Questions regarding Python replace specific texts

I'm writing a script to scrape from another website with Python, and I am facing this question that I have yet to figure out a method to resolve it.
So say I have set to replace this particular string with something else.
word_replace_1 = 'dv'
namelist = soup.title.string.replace(word_replace_1,'11dv')
The script works fine, when the titles are dv234,dv123 etc.
The output will be 11dv234, 11dv123.
However if the titles are, dv234, mixed with dvab123, even though I did not set dvab to be replaced with anything, the script is going to replace it to 11dvab123. What should I do here?
Also, if the title is a combination of alphabits,numbers and Korean characters, say DAV123ㄱㄴㄷ,
how exactly should I make it to only spitting out DAV123, and adding - in between alphabits and numbers?
Python - making a function that would add "-" between letters
This gives me the idea to add - in between all characters, but is there a method to add - between character and number?
the only way atm I can think of is creating a table of replacing them, for example something like this
word_replace_3 = 'a1'
word_replace_4 = 'a2'
.......
and then print them out as
namelist3 = soup.title.string.replace(word_replace_3,'a-1').replace(word_replace_4,'a-2')
This is just slow and not efficient. What would be the best method to resolve this?
Thanks.

Python 3 - string to list (I know it has been asked but I can't get anything to work)

I have an assignment, similar to scrabble. I have to check if a subset is in the set. can only use a letter once. so if subset has 2t and the set has 1t it is false.
My problem is, I used 2 inputs to allow people to enter the subset and set, but that create a string no breaks between the letters which mean split or list won't create a LIST with individual letters. (at least I can't find any way.)
My plan was something like
wordset = word.lower().split()
subset = letters.lower()
for i in range(len(subset)):
if i in subset and in set:
set.remove(i)
I know that properly won't work but until I can get it into a list or someone gives me a hint how to do it with string I can't start testing it. Sorry for so much writing.
If you wish to get a list of characters in a given string you can use a list comprehension:
characters = [x for x in some_string]

String is wrong converted to float? (Blitzmax - Reflections)

I have a little Problem in Blitzmax.
I try to read an INI-file and if I read floats they are converted in a very strange way.
The line in the file which is concerned looks like that for example:
_fStrength=40.6
The Output of this looks like that:
DebugLog:_fStrength: 40.5999985
The code I use to read that works with reflection and looks like that:
For Local fld:TField = EachIn id.EnumFields()
fld.Set(obj, SearchInFile("TempWeapon" + index, fld.Name(), "Weapons.ini"))
DebugLog(fld.Name() + ": " + String(fld.Get(obj)))
Next
I found out, that this only happens if the number after the "." does not equal's 5 or 0.
I can't explain this behaviour, because if I do not use reflections, it works fine.
Could anyone help me please?
As you probably know, your computer stores numbers in binary code, using a limited size. 40.6 expanded in binary is a periodic sequence (101000.1001100110011001100..., infinitely), similarly to what happens when you try to write down the digits of 1/3) repeating and thus can not be represented exactly, so you get rounding errors.
The number of correct digits you get here looks like you are using single-precision floating point numbers, you can push the error further back by going to double, but it won't disappear.
As a reference, you might find Wikipedia on floating point helpful.

Resources