0

Here is my code, in Python:

x=float(raw_input('How many total orders have accumulated: '))
y=float(raw_input('How many units are in the inventory: '))
z=float(raw_input('How many accumulated orders have you placed: '))
print 'order (x/25)+(y/10)+(z/25) units'

I know it's long for a single line of code, but I want it executed in order, like a program. Not step by step where you have to input code in order to conduct the next step. I am writing this to give to a colleague to copy and paste and be able to run the equation hundreds and hundreds of times without writing out the equation each time.

Mind you, the first part of the code works fine, the issue is only with the last part of the code. The print command in the end of that line is:

print 'order (x/25)+(y/10)+(z/25) units'

How do I correctly set up the syntax so it will display the value of that equation rather than just display the equation itself?

I am very new to programming, and only use it as a tool to expedite other endeavors. I am unfamiliar with terminology so I had a hard time finding the information this issue. I searched google and stack exchange, but couldn't find anything.

Jeff LaFay
  • 12,882
  • 13
  • 71
  • 101
01110100
  • 793
  • 4
  • 9
  • 17
  • Might I add one more question? The equation above must be displayed, which user Rohit Jain successfully pointed out in his answer below, but I also must round it to the nearest 5. For instance 137 should be 135, and 138 should be 140. How would I amend the code to round to the nearest 5? – 01110100 Oct 05 '12 at 17:33
  • Note that depending on what Python version you're using, using `/` to divide integers might not do what you expect it to. See [this answer](http://stackoverflow.com/a/183870/1599111) for example. – Lukas Graf Oct 05 '12 at 17:36
  • Also, the parenthesis in `(x/25)+(y/10)+(z/25)` are completely unnecessary. See [order of operations](http://en.wikipedia.org/wiki/Order_of_operations). – Lukas Graf Oct 05 '12 at 17:41
  • Lukas, referring to your first comment about using / to divide integers. In this case, seeing as I want the actual value even if that value has a decimal, is using the single / considered fine? From my understanding of the example you posted, using a single / to divide provides the float where as using // to divide provides a "real" number. Is that correct? – 01110100 Oct 05 '12 at 17:45
  • It depends on the Python version, the behavior changed with Python 3.x. Which version are you (resp. your colleague) using? – Lukas Graf Oct 05 '12 at 17:47
  • Version: 7.3-2 (32-bit), so I would imagine that it applies to our version? – 01110100 Oct 05 '12 at 17:48
  • 7.3-2 is not a Python version. Do you mean Python-2.7.3-r2? – Lukas Graf Oct 05 '12 at 17:50
  • Absolutely. My apologies, I just copied that from Terminal and figured that was the version of Python. Below that I noticed it says: "Python 2.7.3" – 01110100 Oct 05 '12 at 17:51
  • Added my comments about integer division to the answer for better readability. – Lukas Graf Oct 05 '12 at 18:14
  • For rounding to 5 see this answer: [Round to 5(or other number) in python](http://stackoverflow.com/a/2272174/1599111). – Lukas Graf Oct 05 '12 at 18:18

3 Answers3

4

Separate various elements using comma(,), that will concatenate them.. After evaluating any expression present..

So, your expression will be evaluated, then the value obtained will be concatenated with the strings before it and after it, using comma(,)..

** EDIT: - Actually, you don't need those parenthesis..

print 'order:', x/25 + y/10 + z/25 ,' units'
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
3

The alternative to @Rohit's answer is using string formatting:

print 'order %s units' % (x / 25 + y / 10 + z / 25)

Or using the newer str.format() method:

print 'order {0} units'.format(x / 25 + y / 10 + z / 25)

Edit: Including my comments about integer division here, for better readability.

Depending on your Python version, dividing two integers by using / might not do what you expect it to.

In Python versions earlier to 3.x the result of dividing two integers will always be an integer as well (rounded down, called "floor division").

Python 2.7.2 (default, Oct 30 2011, 13:00:18)
>>> 5 / 2
2

So 5 / 2 == 2. If you want to do "true division" you need to convert one of the arguments to a float first:

Python 2.7.2 (default, Oct 30 2011, 13:00:18) 
>>> 5 / 2.0
2.5
>>> 5 / 2.0 == 2.5
True

With Python 3.0 the behavior of the / operator changed:

Python 3.2.2 (default, Apr 15 2012, 16:59:18) 
>>> 5 / 2
2.5

Converting to a float can be done by using float(i).

For all the details, reasons and to read about from __future__ import division, see PEP238 and this article for example.

Lukas Graf
  • 30,317
  • 8
  • 77
  • 92
  • @Rohit Your answer works just fine for this example. But for anything more than `print a,b,c` it's usually better to use string formatting, yes :) – Lukas Graf Oct 05 '12 at 17:45
  • Lukas, great response. Definitely something to note. Like I said I am very new to programing so this concept is actually a bit difficult for me to grasp, but I will definitely refer to it when more complex issues arise in my code. +1 – 01110100 Oct 05 '12 at 17:47
  • 1
    @Tyler.. String.format() is the easiest way you can format your string.. And it is also prefered. But since you are at the beginning, you can get started with whatever makes you feel comfortable.. But ultimately its `String.format()`.. – Rohit Jain Oct 05 '12 at 17:52
  • ummm, @RohitJain, I would argue that f-strings are easier as you don't have to go through the hassle of using the format function, but I can see why you would say that. – theProCoder Sep 20 '21 at 13:33
0

I see you are using Python 2, as Python 3 requires brackets for the print statement, like this print().

I highly recommend you move on to Python 3, as it is the newest version and Python 2 is no longer supported and updated. The latest version of Python, at the time of writing at least, is Python 3.9.2 .

Like @Lukas Graf said, you can use string formatting. The benefit of Python 3.6 and above is f-strings. It is like str.format() but even better. To use f-strings, you prefix the string with an f and then just surround the actual code in curly braces ({})

print(f'order {(x / 25) + (y / 10) + (z / 250)} units')

Hope this helps. (I know, I am 9 years late)

theProCoder
  • 390
  • 6
  • 21