Jump to content
  • Advertisement
Sign in to follow this  
Landshark

Python 2.6.2 Very Beginner questions

This topic is 3232 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Intro: Hey, what's up. I'm learning python (still...). I'm using 2.6.2 and running IDLE for the coding part. I've been following a very easy to follow tutorial on youtube to get me started. This is the 2nd time I've tried to start learning Python, but I feel much less confused so far and really am enjoy it. Forgive me if I use the wrong coding terms, I'm still learning! Anyway, my goal for myself this week is to produce a very simple, yet functional and useful (to me) program. Problem: I am writing a program to calculate how much a stock price must rise in order to meet your profit goal. It's pretty simple math. Basically, the program takes in the users cash they want to spend, price of stock, how much money they want to make 'profit' from the stock and any fees associated with the stock transaction (broker fees). It then tells them how much the stock must rise in price before they can sell and meet their profit goal. Here's the code so far (minus all the math stuff, which will probably be posted later when/if I have problems with it)
cash=input("How much money do you want to spend? ")
stockprice=input("What is the share price of the stock? ")
cashgoal=input("How much money do you want to make as profit? ")
stockfees=input("What is the stock transaction fee (if applicable)? ")
print ""
print "This is how much money you have to spend: $%i" % cash
print "This is how much each share costs: $%i" % stockprice
print "This is how much you want to make: $%i" % cashgoal
print "This is how much each stock transaction costs: $%i" % stockfees
The problem I'm having is working with decimal numbers. For the stock price, if I use a number such as 0.179, when 'print' the info back to the user (this is just for testing purposes now) it returns a '0' value. But, this only happens when I print the info using a %i in the string. If I just print the integer without using the %i in the string:
print stockprice
Then it will return the correct value of '0.179'. I'm missing the info on how to convert the 'stockprice' into something the %i can handle correctly. Or maybe I shouldn't be using %i for decimals...or maybe I need to take the 'stockprice' after the user has given input and convert it to something else before using the %i in the str. Anyway, help please! :) ps: what's the correct way to post formatted code? I used [ code ] and [ /code ], but I don't see what it did exactly.

Share this post


Link to post
Share on other sites
Advertisement
Programmer's skill #1: looking things up in the documentation

http://docs.python.org/library/stdtypes.html#string-formatting-operations

Share this post


Link to post
Share on other sites
%i prints an integer, you want a float %0.2f which displays a float number to the 2nd decimal place.

Share this post


Link to post
Share on other sites
So yeah, you want to be using %f for floating point values (numbers that we normally consider to be decimals). Binary Floating Point numbers represent a standard and efficient way for a machine to store and use decimal numbers, unfortunately the price for this efficiency is that we sacrifice precision.

Python does support decimal (as opposed to binary) floating point numbers and arithmetic, which if you're interested in learning about you can read more here. We use regular binary floating point values 99% of the time and decimals floating point values for the 1% where precision is paramount (you don't want to be losing money in your bank account everytime the bank do their account processing just because of a precision issue).

As for [ code ] tags, they make it so that:
1) Each symbol has the same width, which makes aligning text across multiple lines easy, and
2) Consecutive spaces are preserved when you post. Normally multiple spaces get collapsed into a single space.

You can also use the [ source lang="python" ] [ /source ] tags to get syntax highlighting in an embedded scrollable box.
Also, to get this inline pre-formatted text you can use < tt > < /tt > tags, they're different from code tags in that they can be used on a single line amongst regular text and they don't fulfill #2 above, so spaces are still collapsed. Incidentally code tags are really just < pre > < /pre > tags. [smile]

Share this post


Link to post
Share on other sites
You guys rock, thank you so much for the help! That's why I posted here. :)

I suspect it would make more sense for me to use the %0.2f for all my values to help show dollar and change amounts.

I'll read through the docs referenced above. I promise I DID do a lot of searching for the answer, but I think I worded my searches wrong. I was looking for integer to decimal conversions and all I got was integer to binary stuff which didn't seem applicable to my question.

Anyway, thank you all!

Share this post


Link to post
Share on other sites
As an aside, I almost never use input, this evaluates the user's input almost like a piece of python code; the user could run riot this. As an example try typing in cash as the response when it asks you about profit and see what happens. Then imagine if you had a function in your code for transacting money to and fro between bank accounts and you were to let the user get wind of this function's name.. [tears]

In place of that you can intead use either raw_input or sys.stdin.readline (if you import sys first). These return your response as a regular string with no evaluating going on whatsoever, so they're safe. If you want to get a user's input as a number rather than a string then you can cast it:

cash = float(raw_input("How much money do you want to spend? "))

(If you want an integer then use int(..) instead).

Note that in Python 3, raw_input has been renamed to input and the function previously known as input has been dropped.

Share this post


Link to post
Share on other sites
I won't fault you for using wrong terminology, but you really ought to have noticed that there's a whole separate forum for beginner questions. :) Moved.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
I won't fault you for using wrong terminology, but you really ought to have noticed that there's a whole separate forum for beginner questions. :) Moved.


Noted. :) Thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by dmatter
As an aside, I almost never use input, this evaluates the user's input almost like a piece of python code; the user could run riot this. As an example try typing in cash as the response when it asks you about profit and see what happens. Then imagine if you had a function in your code for transacting money to and fro between bank accounts and you were to let the user get wind of this function's name.. [tears]

In place of that you can intead use either raw_input or sys.stdin.readline (if you import sys first). These return your response as a regular string with no evaluating going on whatsoever, so they're safe. If you want to get a user's input as a number rather than a string then you can cast it:

cash = float(raw_input("How much money do you want to spend? "))

(If you want an integer then use int(..) instead).

Note that in Python 3, raw_input has been renamed to input and the function previously known as input has been dropped.


Dmatter,

As far as learning goes and what I should take with me from your post: It looks like best practice, if you know the input data type before-hand, is to only ask for that type of data. Hence using cash = float(raw_input(..)) Because I know it's going to be a float then I should only ask for a float. I'll keep that in mind from now on. Thanks for your help.

Share this post


Link to post
Share on other sites
I do my pyhton input like this:

some_name = int(raw_input("What is your name?")

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!