Just wrote my first ever program (few questions)

  • Thread starter Thread starter Couperin
  • Start date Start date
  • Tags Tags
    Program
Click For Summary
SUMMARY

The discussion centers around a beginner's experience with Python, specifically creating a program that calculates the intersection points between a line and a quadratic curve using the discriminant method. The user seeks advice on converting their script into a Windows application and receives recommendations for using tools like py2exe to bundle Python scripts into executable files. Additionally, the conversation highlights the importance of using safe input methods, such as raw_input(), to prevent code injection vulnerabilities. The community emphasizes the need for proper error handling and suggests breaking the program into functions for better organization.

PREREQUISITES
  • Basic understanding of Python programming (Python 3.x)
  • Knowledge of quadratic equations and discriminants
  • Familiarity with input handling and error management in Python
  • Understanding of converting Python scripts to executable files using py2exe
NEXT STEPS
  • Learn how to use py2exe for creating standalone Windows applications from Python scripts
  • Explore Python's raw_input() function for safer user input handling
  • Study how to implement functions in Python to organize code effectively
  • Investigate additional Python libraries for GUI development, such as Tkinter or PyQt
USEFUL FOR

Beginner Python programmers, educators teaching programming concepts, and developers interested in converting Python scripts into executable applications.

Couperin
Messages
59
Reaction score
0
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?
 
Technology news on Phys.org
You could always work your way through http://mitpress.mit.edu/sicp/full-text/book/book.html... <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.
 
Just for the record, the first program written must always be "Hello world!"
 
Jheriko said:
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/" .
 
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.

Jheriko said:
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.

Jheriko said:
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.

Jheriko said:
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:

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 7 ·
Replies
7
Views
1K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 20 ·
Replies
20
Views
33K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 16 ·
Replies
16
Views
3K