Have you ever wondered how to make a "notepad" in Python? Well I will show you how to make the very basics. Here we go.
So the first thing we want to do is a vairable that eqauls whatever we write. That way it will be easier to save it later:
a = input()
This is like what we did with the Pyculator, only with different uses.
Now we would be able to wright whatever we like and it would eqaul a.
Now we want to save what we wrote to a file off you choice. You can either save it as a .txt file or something random like .Alosisthebest.
This is how we do it
out_file = open("Pynote.pp", "wt")
out_file.write(a)
out_file.close()
Now we can see that every line has a out_file, all it does is it saves all this data, just in different ways.
Line 1: Tells Python what to save as (name .(name))
Line 2: Tells Python what in the code is going to be saved.
Line 3: Tells Python to not save anything more.
Now if we sve it as a file that can't be opened with any other program, we need to make a program that can open it.
It's pretty simple:
in_file = open("Pynote.pp", "rt")
text = in_file.read()
in_file.close()
print(text)
Let me explain this very simple code:
Line 1: Locate a file of a specific name and tells the next line to read it.
Line 2: Reads the text that the previous line located, but it doesn't show it.
Line 3: Tells Python to not locate or read any more files.
Line 5: Prints the text that the second line read.
Pictures showing:
So there you go, the very basics of a notepad.
Hope you develope this code to work even better. If you do please mail it to me at: NerdyAlos@gmail.com
This is not tutorials. Neither is it questions. This is a site to just help each other develope their Python skills and make Python programs (or what I like to call it, "Pyprograms") with the skills they have!
Monday, March 18, 2013
Sunday, March 17, 2013
Python + Calculator = Pyculator
A calculator in Python is not the hardest thing to do, but neither is it the easiest.
There are several ways to do this, but I only know two of them:
There are several ways to do this, but I only know two of them:
The first way to do it is to choose before if want to subtract, add, multiply or use division and them just go there. That would maybe work in Python 2, but not in Python 3. Because in Python two there are ways to go to another part of the script, but not in Python 3, or at least not a real documented way.
That's why I use the second method:
To have three variables:
a
b
c
It's important to have them under each other, otherwies you would only be able to put in one digit numbers.
Ofcourse, this method have some bad parts to. You'll have to press enter afrter every number you put in:
55
<Enter>
+
<Enter>
55
<Enter>
110
But let's move on.
after a b and c you have to put input () so you can put a number in there:
a = input()
b = input()
c = input()
Now when we have the variables equals whatever we want, we need to make them interact mathematically. How do we do that? Well, we have to tell Python that when "b" = +,-,*,/ it should either +,-,*,/ depending on what you puted in.
So we do that by using the "if statement". We need to tell python if, for example, "b" = + it should add "a" and "c" togehter:
if b >= "+" :
print(int(a) + int(c))
Okay, let me explain the code for you.
Line 1: This line basically tells Python if b = "+" it should do whatever is under it. We use the "" to say that + is a string and not a normal number.
Line 2: First of, make sure you have four spaces after a "if statement". Otherwise Python will give you errors. Okay now to the actual code. "Print" outputs whatever is in the (). In this case it should output a + c. As you can see we use int() before them. All int() does is it defines whatever is in the () is a number and not a letter.
Do this whit all of the other mathematic characters. But be aware, Python doesn't do integer division by default, so you naeed to tell python that a and c are decimals or otherwise it won't divide.
Also, after the if commando, use elif, otherwise it won't work.
Here is my code:
As you can see I didn't have division because for what I will use my Pycualtor I won't need it.
Also at the end of the code you can see k=input("Press <Enter> to exit"). I use so my Pyculator won't close right after I wrote my number. If you are on Linux you don't need this.
And here is the result:
Hard to see I know.
Hope you learned something, and please develope the Pyculator even more. Send update to NerdyAlos@gmail.com! Thank you!
Subscribe to:
Posts (Atom)





