| Thread Closed |
Just wrote my first ever program! (few questions) |
Share Thread | Thread Tools |
| Jan10-07, 04:38 PM | #1 |
|
|
Just wrote my first ever program! (few questions)
Someone told me that Python is a good first language to learn, so I started learning it yesterday. I think I might fall in love with programming as time goes by...
Anyway, here's my first program. What it does is ask you for a line equation, and then a quadratic curve equation. It then tells you how many times the two lines meet, by first subtracting the line equation values from the curve equation values, and then finding the amount of roots by using the discriminant. I plan on adding some sort of function that shows you the coordinates of these lines of intersection. Also, it cannot take line equations of the form x = n (ie, vertical lines), because of the cheap way I've programmed it. I was wondering how I would take this program and make a Windows application out of it? Anyways, just thought you might be interested! Here's the code: Code:
# Discriminant finder
print "This program will tell you how many time a line crosses a quadratic curve."
print "Firstly, I need information about your line equation."
lm = input("What is its gradient? ")
lc = input("And what is its y-intercept? ")
print "Your line equation is y =", lm, "x +", lc
print "Now the curve."
qa = input("Firstly, what is its a value? ")
qb = input("And its b value? ")
qc = input("And its c value? ")
print "Thanks."
sb = qb-lm
sc = qc-lc
disc = (sb**2)-4*(qa*sc)
print "The discriminant is equal to ", disc
if disc > 0:
print "The line crosses the curve twice."
elif disc == 0:
print "The line is a tangent to the curve."
elif disc < 0:
print "The line doesn't cross the curve."
|
| Jan11-07, 07:14 AM | #3 |
|
|
You need to find a Python compiler to make a Windows application out of this.
If you want to make programs rather than scripts I would recommend using a language which is more geared towards full application development. Visual Basic or C# would be the easy choices and are quite good for beginners, C++ if you want to understand what is really happening. Python is more for writing scripts to be run as part of another program, although I think there are compilers out there my 3 minutes of searching turned up nothing. |
| Jan11-07, 10:21 AM | #4 |
|
|
Just wrote my first ever program! (few questions)
Just for the record, the first program written must always be "Hello world!"
|
| Jan12-07, 10:59 AM | #5 |
|
|
|
| Jan14-07, 06:02 PM | #6 |
|
|
You can download py2exe, it makes python files executable.
You can download it from http://www.py2exe.org/. |
| Jan17-07, 05:40 PM | #7 |
|
|
I'm sorry, but there's too much false information in this thread. I'm going to try to clear some things up, so people don't run off with the wrong idea about things.
To make an application, you need something to bundle the byte-compiled code with the Python dlls, then package the other libraries. It's not really an application, since you've shipped the entire dependancy with you ... But it's as close as you can get. Loismustdie pointed you to the program perfect for this task. And additionally, the interpreter is available for download here. And again, there's not a compiler for Python. Python is only semi-compiled into a byte-code, which is interpreted by the Python interpreter. It can not produce an exe. All you can do with it is include it in your Python script, and witness a speedup. |
| Jan17-07, 06:04 PM | #8 |
|
|
Now Couperin, about your program. If you want to make this an "application", check out the link that Loismustdie provided. This will bundle the Python interpreter with the byte-code from your source code.
You will want to add a prompt to the end of your script, so that it doesn't immediately close after reaching the final line. If you don't prompt the user for anything, the window will immediately close after the final line. There are a couple things I should point out about your code. input() is generally a bad function to use, because it is fed to eval() before it is handed back to you. People can exploit this and enter malicious code into the prompt. It is a better idea to use raw_input() and convert to an integer. If the user didn't enter a number, "except" the error, and ask for it again. This is the safest way to do what you want. Code:
x = raw_input('Enter an integer: ') # request a string
try:
new_x = int(x) # convert the input to an integer
except ValueError: # this block is accessed if the integer conversion fails
print 'Please enter an integer. Not a string.'
Code:
while 1: # loop infinitely
x = raw_input('Enter an integer: ') # request a string
try:
new_x = int(x) # convert the input to an integer
break # the conversion was successfull: break out of the loop
except ValueError: # this block is accessed if the integer conversion fails
print 'Please enter an integer. Not a string.'
Code:
def int_input(prompt):
while 1: # loop infinitely
x = raw_input('Enter an integer: ') # request a string
try:
new_x = int(x) # convert the input to an integer
break # the conversion was successfull: break out of the loop
except ValueError: # this block is accessed if the integer conversion fails
print 'Please enter an integer. Not a string.'
return new_x # return the integer
Code:
x = int_input('Enter an integer: ')
Rewriting this code using the "safer" method of grabbing an integer is simple as well. All that's changed is every 'input' is turned into 'int_input'. Code:
def int_input(prompt):
while 1: # loop infinitely
x = raw_input('Enter an integer: ') # request a string
try:
new_x = int(x) # convert the input to an integer
break # the conversion was successfull: break out of the loop
except ValueError: # this block is accessed if the integer conversion fails
print 'Please enter an integer. Not a string.'
return new_x # return the integer
# Discriminant finder
print "This program will tell you how many time a line crosses a quadratic curve."
print "Firstly, I need information about your line equation."
lm = int_input("What is its gradient? ")
lc = int_input("And what is its y-intercept? ")
print "Your line equation is y =", lm, "x +", lc
print "Now the curve."
qa = int_input("Firstly, what is its a value? ")
qb = int_input("And its b value? ")
qc = int_input("And its c value? ")
print "Thanks."
sb = qb-lm
sc = qc-lc
disc = (sb**2)-4*(qa*sc)
print "The discriminant is equal to ", disc
if disc > 0:
print "The line crosses the curve twice."
elif disc == 0:
print "The line is a tangent to the curve."
elif disc < 0:
print "The line doesn't cross the curve."
In the mean time, you could also check out http://www.pythonchallenge.com/. Cheers, and Happy Coding, - Sane |
| Thread Closed |
| Thread Tools | |
Similar Threads for: Just wrote my first ever program! (few questions)
|
||||
| Thread | Forum | Replies | ||
| who wrote linux | Computing & Technology | 3 | ||
| Creationism book, who wrote ? | Biology | 5 | ||
| Questions about a MS program in chemistry | Academic Guidance | 0 | ||
| Guess who wrote this? | General Discussion | 73 | ||
| Why not Jesus wrote his own biography ? | General Discussion | 7 | ||