Set discounted ticket price, with a floor of 0 [closed]











up vote
0
down vote

favorite
1












Is there a shorter way to write this?



discounted_price = original_ticket_price - discount_value
if discounted_price < 0:
discounted_price = 0









share|improve this question















closed as off-topic by Sᴀᴍ Onᴇᴌᴀ, Martin R, Stephen Rauch, IEatBagels, Toby Speight Nov 27 at 18:12


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Sᴀᴍ Onᴇᴌᴀ, Martin R, Stephen Rauch, IEatBagels, Toby Speight

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    Why would you want a shorter way to write it if you don't mind? Are you golfing or something?
    – Julien Rousé
    Nov 27 at 16:03










  • What's the connection to golfing? I just want to learn it the best, shortest way. Thought there might be a better way to write it.
    – Jon Programmer
    Nov 27 at 16:12










  • It's just that usually when people ask for the shortest way to write something it's for code golf
    – Julien Rousé
    Nov 27 at 16:19






  • 1




    Ahh, never heard of that. No, I actually just try to learn it the 'right' way.
    – Jon Programmer
    Nov 27 at 16:40










  • Keep asking question then ;) Next time you post here, copy a more sizeable snippet of code, feedbacks will be way more interesting.
    – Julien Rousé
    Nov 27 at 16:50















up vote
0
down vote

favorite
1












Is there a shorter way to write this?



discounted_price = original_ticket_price - discount_value
if discounted_price < 0:
discounted_price = 0









share|improve this question















closed as off-topic by Sᴀᴍ Onᴇᴌᴀ, Martin R, Stephen Rauch, IEatBagels, Toby Speight Nov 27 at 18:12


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Sᴀᴍ Onᴇᴌᴀ, Martin R, Stephen Rauch, IEatBagels, Toby Speight

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    Why would you want a shorter way to write it if you don't mind? Are you golfing or something?
    – Julien Rousé
    Nov 27 at 16:03










  • What's the connection to golfing? I just want to learn it the best, shortest way. Thought there might be a better way to write it.
    – Jon Programmer
    Nov 27 at 16:12










  • It's just that usually when people ask for the shortest way to write something it's for code golf
    – Julien Rousé
    Nov 27 at 16:19






  • 1




    Ahh, never heard of that. No, I actually just try to learn it the 'right' way.
    – Jon Programmer
    Nov 27 at 16:40










  • Keep asking question then ;) Next time you post here, copy a more sizeable snippet of code, feedbacks will be way more interesting.
    – Julien Rousé
    Nov 27 at 16:50













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





Is there a shorter way to write this?



discounted_price = original_ticket_price - discount_value
if discounted_price < 0:
discounted_price = 0









share|improve this question















Is there a shorter way to write this?



discounted_price = original_ticket_price - discount_value
if discounted_price < 0:
discounted_price = 0






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 at 16:52









200_success

128k15149412




128k15149412










asked Nov 27 at 16:00









Jon Programmer

1154




1154




closed as off-topic by Sᴀᴍ Onᴇᴌᴀ, Martin R, Stephen Rauch, IEatBagels, Toby Speight Nov 27 at 18:12


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Sᴀᴍ Onᴇᴌᴀ, Martin R, Stephen Rauch, IEatBagels, Toby Speight

If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by Sᴀᴍ Onᴇᴌᴀ, Martin R, Stephen Rauch, IEatBagels, Toby Speight Nov 27 at 18:12


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Sᴀᴍ Onᴇᴌᴀ, Martin R, Stephen Rauch, IEatBagels, Toby Speight

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1




    Why would you want a shorter way to write it if you don't mind? Are you golfing or something?
    – Julien Rousé
    Nov 27 at 16:03










  • What's the connection to golfing? I just want to learn it the best, shortest way. Thought there might be a better way to write it.
    – Jon Programmer
    Nov 27 at 16:12










  • It's just that usually when people ask for the shortest way to write something it's for code golf
    – Julien Rousé
    Nov 27 at 16:19






  • 1




    Ahh, never heard of that. No, I actually just try to learn it the 'right' way.
    – Jon Programmer
    Nov 27 at 16:40










  • Keep asking question then ;) Next time you post here, copy a more sizeable snippet of code, feedbacks will be way more interesting.
    – Julien Rousé
    Nov 27 at 16:50














  • 1




    Why would you want a shorter way to write it if you don't mind? Are you golfing or something?
    – Julien Rousé
    Nov 27 at 16:03










  • What's the connection to golfing? I just want to learn it the best, shortest way. Thought there might be a better way to write it.
    – Jon Programmer
    Nov 27 at 16:12










  • It's just that usually when people ask for the shortest way to write something it's for code golf
    – Julien Rousé
    Nov 27 at 16:19






  • 1




    Ahh, never heard of that. No, I actually just try to learn it the 'right' way.
    – Jon Programmer
    Nov 27 at 16:40










  • Keep asking question then ;) Next time you post here, copy a more sizeable snippet of code, feedbacks will be way more interesting.
    – Julien Rousé
    Nov 27 at 16:50








1




1




Why would you want a shorter way to write it if you don't mind? Are you golfing or something?
– Julien Rousé
Nov 27 at 16:03




Why would you want a shorter way to write it if you don't mind? Are you golfing or something?
– Julien Rousé
Nov 27 at 16:03












What's the connection to golfing? I just want to learn it the best, shortest way. Thought there might be a better way to write it.
– Jon Programmer
Nov 27 at 16:12




What's the connection to golfing? I just want to learn it the best, shortest way. Thought there might be a better way to write it.
– Jon Programmer
Nov 27 at 16:12












It's just that usually when people ask for the shortest way to write something it's for code golf
– Julien Rousé
Nov 27 at 16:19




It's just that usually when people ask for the shortest way to write something it's for code golf
– Julien Rousé
Nov 27 at 16:19




1




1




Ahh, never heard of that. No, I actually just try to learn it the 'right' way.
– Jon Programmer
Nov 27 at 16:40




Ahh, never heard of that. No, I actually just try to learn it the 'right' way.
– Jon Programmer
Nov 27 at 16:40












Keep asking question then ;) Next time you post here, copy a more sizeable snippet of code, feedbacks will be way more interesting.
– Julien Rousé
Nov 27 at 16:50




Keep asking question then ;) Next time you post here, copy a more sizeable snippet of code, feedbacks will be way more interesting.
– Julien Rousé
Nov 27 at 16:50










2 Answers
2






active

oldest

votes

















up vote
10
down vote



accepted










Use the built-in max() function:



discounted_price = max(0, original_ticket_price - discount_value)





share|improve this answer




























    up vote
    3
    down vote













    Python has a syntax for ternary since 2.5.



    Syntax for it is a if condition else b, where condition is Truthy or Falsy( see Graipher comment).



    You can rewrite your code like:



    discounted_price = original_ticket_price - discount_value
    discounted_price if discounted_price >= 0 else 0


    Personnaly I find it harder to read but it is another way to write your code.



    EDIT you can find more info on ternary in this SO post






    share|improve this answer



















    • 1




      condition does not have to be a boolean (True or False), a truthy or falsy value is sufficient ([0] and also work), as almost always in Python.
      – Graipher
      Nov 27 at 17:24


















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    10
    down vote



    accepted










    Use the built-in max() function:



    discounted_price = max(0, original_ticket_price - discount_value)





    share|improve this answer

























      up vote
      10
      down vote



      accepted










      Use the built-in max() function:



      discounted_price = max(0, original_ticket_price - discount_value)





      share|improve this answer























        up vote
        10
        down vote



        accepted







        up vote
        10
        down vote



        accepted






        Use the built-in max() function:



        discounted_price = max(0, original_ticket_price - discount_value)





        share|improve this answer












        Use the built-in max() function:



        discounted_price = max(0, original_ticket_price - discount_value)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 at 16:55









        200_success

        128k15149412




        128k15149412
























            up vote
            3
            down vote













            Python has a syntax for ternary since 2.5.



            Syntax for it is a if condition else b, where condition is Truthy or Falsy( see Graipher comment).



            You can rewrite your code like:



            discounted_price = original_ticket_price - discount_value
            discounted_price if discounted_price >= 0 else 0


            Personnaly I find it harder to read but it is another way to write your code.



            EDIT you can find more info on ternary in this SO post






            share|improve this answer



















            • 1




              condition does not have to be a boolean (True or False), a truthy or falsy value is sufficient ([0] and also work), as almost always in Python.
              – Graipher
              Nov 27 at 17:24















            up vote
            3
            down vote













            Python has a syntax for ternary since 2.5.



            Syntax for it is a if condition else b, where condition is Truthy or Falsy( see Graipher comment).



            You can rewrite your code like:



            discounted_price = original_ticket_price - discount_value
            discounted_price if discounted_price >= 0 else 0


            Personnaly I find it harder to read but it is another way to write your code.



            EDIT you can find more info on ternary in this SO post






            share|improve this answer



















            • 1




              condition does not have to be a boolean (True or False), a truthy or falsy value is sufficient ([0] and also work), as almost always in Python.
              – Graipher
              Nov 27 at 17:24













            up vote
            3
            down vote










            up vote
            3
            down vote









            Python has a syntax for ternary since 2.5.



            Syntax for it is a if condition else b, where condition is Truthy or Falsy( see Graipher comment).



            You can rewrite your code like:



            discounted_price = original_ticket_price - discount_value
            discounted_price if discounted_price >= 0 else 0


            Personnaly I find it harder to read but it is another way to write your code.



            EDIT you can find more info on ternary in this SO post






            share|improve this answer














            Python has a syntax for ternary since 2.5.



            Syntax for it is a if condition else b, where condition is Truthy or Falsy( see Graipher comment).



            You can rewrite your code like:



            discounted_price = original_ticket_price - discount_value
            discounted_price if discounted_price >= 0 else 0


            Personnaly I find it harder to read but it is another way to write your code.



            EDIT you can find more info on ternary in this SO post







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 27 at 18:05

























            answered Nov 27 at 16:22









            Julien Rousé

            694518




            694518








            • 1




              condition does not have to be a boolean (True or False), a truthy or falsy value is sufficient ([0] and also work), as almost always in Python.
              – Graipher
              Nov 27 at 17:24














            • 1




              condition does not have to be a boolean (True or False), a truthy or falsy value is sufficient ([0] and also work), as almost always in Python.
              – Graipher
              Nov 27 at 17:24








            1




            1




            condition does not have to be a boolean (True or False), a truthy or falsy value is sufficient ([0] and also work), as almost always in Python.
            – Graipher
            Nov 27 at 17:24




            condition does not have to be a boolean (True or False), a truthy or falsy value is sufficient ([0] and also work), as almost always in Python.
            – Graipher
            Nov 27 at 17:24



            Popular posts from this blog

            Plaza Victoria

            In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

            How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...