# Just wrote my first ever program! (few questions)

## Main Question or Discussion Point

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."
I need more ideas for programs, otherwise I can't practice! Any cool ideas?

## Answers and Replies

Related Programming and Computer Science News on Phys.org
verty
Homework Helper
You could always work your way through SICP... <evil grin>

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.

DaveC426913
Gold Member
Just for the record, the first program written must always be "Hello world!"

You need to find a Python compiler to make a Windows application out of this.
Look for Psyco. It seems to be a compiler, so I guess it can produce a stand-alone .exe

You can download py2exe, it makes python files executable.

You can download it from http://www.py2exe.org/" [Broken].

Last edited by a moderator:
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.

You need to find a Python compiler to make a Windows application out of this.
Python's not a compiled language, it's interpreted. You need the Python interpreter to execute the Python script.

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.

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.
Run as part of another program? I'm not sure what that's supposed to mean, but I'll assume you meant to say something more sensical. Blender, for instance, a great 3d modelling program, was programmed entirely in Python.

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.

Look for Psyco. It seems to be a compiler, so I guess it can produce a stand-alone .exe
No, it's not a compiler. It's an optimizer. It optimizes some of the bytecode by replacing expressions with constants, and doing certain optimizations to functions that are more regularly called.

It can not produce an exe. All you can do with it is include it in your Python script, and witness a speedup.

Last edited:
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.'
Since you would want to keep prompting the user until he inputs a real integer, you could stick this into a loop.

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.'
Then, furthermore, you could throw this into a function so that you can reuse it without your code getting messy and repetitive. I'll expect you have not yet learned functions yet, but I'll just toss this your way now. Maybe you can swallow it, if you can't, there's no problem. You'll be able to come back to this when you're learning functions, and understand it completely.

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
The function could then be used just as 'raw_input' and 'input' are used:

Code:
x = int_input('Enter an integer: ')
Simple huh?

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."
Once you start learning about functions, I suggest that you come back to this and break your program into functions for practice. Have a main function that calls a 'discriminant' by passing in the user-inputted values and return the 'disc' variable.

In the mean time, you could also check out http://www.pythonchallenge.com/.

Cheers, and Happy Coding,
- Sane

Last edited: