Just wrote my first ever program (few questions)

  • Thread starter Couperin
  • Start date
  • Tags
    Program
In summary: First, 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. Secondly, if you want to make this a Windows application, you will need to find a compiler for Python.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.
  • #1
Couperin
59
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
  • #2
You could always work your way through http://mitpress.mit.edu/sicp/full-text/book/book.html... <evil grin>
 
  • #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.
 
  • #4
Just for the record, the first program written must always be "Hello world!"
 
  • #5
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
 
  • #6
You can download py2exe, it makes python files executable.

You can download it from http://www.py2exe.org/" .
 
Last edited by a moderator:
  • #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.

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:
  • #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.'

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:

What is a program and why is it important?

A program is a set of instructions or code that tells a computer what to do. It is important because it allows us to automate tasks and solve complex problems efficiently.

How do I write a program?

To write a program, you need to first choose a programming language and familiarize yourself with its syntax and rules. Then, you can use a text editor or an integrated development environment (IDE) to write your code. Finally, you can compile or run your code to see the output.

What is the difference between a compiler and an interpreter?

A compiler translates the entire program into machine code before running it, while an interpreter translates and executes the code line by line. This means that a compiled program usually runs faster, but an interpreted program can be more interactive and easier to debug.

How can I improve my programming skills?

Practice is key to improving your programming skills. Start with simple programs and gradually work your way up to more complex ones. It is also helpful to read and learn from other people's code, participate in coding challenges, and take online courses or tutorials.

What are some common mistakes to avoid when writing a program?

Some common mistakes to avoid when writing a program include not following proper syntax, not testing your code as you go, not using proper variable names, and not commenting your code for better readability. It is also important to have a plan and structure before starting to code, and to always double check your code for errors.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Introductory Physics Homework Help
Replies
10
Views
1K
  • General Math
Replies
16
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
Back
Top