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











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















put on hold as off-topic by Sᴀᴍ Onᴇᴌᴀ, Martin R, Stephen Rauch, IEatBagels, Toby Speight 2 days ago


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é
    2 days ago










  • 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
    2 days ago










  • It's just that usually when people ask for the shortest way to write something it's for code golf
    – Julien Rousé
    2 days ago






  • 1




    Ahh, never heard of that. No, I actually just try to learn it the 'right' way.
    – Jon Programmer
    2 days ago










  • Keep asking question then ;) Next time you post here, copy a more sizeable snippet of code, feedbacks will be way more interesting.
    – Julien Rousé
    2 days ago















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















put on hold as off-topic by Sᴀᴍ Onᴇᴌᴀ, Martin R, Stephen Rauch, IEatBagels, Toby Speight 2 days ago


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é
    2 days ago










  • 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
    2 days ago










  • It's just that usually when people ask for the shortest way to write something it's for code golf
    – Julien Rousé
    2 days ago






  • 1




    Ahh, never heard of that. No, I actually just try to learn it the 'right' way.
    – Jon Programmer
    2 days ago










  • Keep asking question then ;) Next time you post here, copy a more sizeable snippet of code, feedbacks will be way more interesting.
    – Julien Rousé
    2 days ago













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 2 days ago









200_success

127k15148412




127k15148412










asked 2 days ago









Jon Programmer

1154




1154




put on hold as off-topic by Sᴀᴍ Onᴇᴌᴀ, Martin R, Stephen Rauch, IEatBagels, Toby Speight 2 days ago


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.




put on hold as off-topic by Sᴀᴍ Onᴇᴌᴀ, Martin R, Stephen Rauch, IEatBagels, Toby Speight 2 days ago


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é
    2 days ago










  • 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
    2 days ago










  • It's just that usually when people ask for the shortest way to write something it's for code golf
    – Julien Rousé
    2 days ago






  • 1




    Ahh, never heard of that. No, I actually just try to learn it the 'right' way.
    – Jon Programmer
    2 days ago










  • Keep asking question then ;) Next time you post here, copy a more sizeable snippet of code, feedbacks will be way more interesting.
    – Julien Rousé
    2 days ago














  • 1




    Why would you want a shorter way to write it if you don't mind? Are you golfing or something?
    – Julien Rousé
    2 days ago










  • 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
    2 days ago










  • It's just that usually when people ask for the shortest way to write something it's for code golf
    – Julien Rousé
    2 days ago






  • 1




    Ahh, never heard of that. No, I actually just try to learn it the 'right' way.
    – Jon Programmer
    2 days ago










  • Keep asking question then ;) Next time you post here, copy a more sizeable snippet of code, feedbacks will be way more interesting.
    – Julien Rousé
    2 days ago








1




1




Why would you want a shorter way to write it if you don't mind? Are you golfing or something?
– Julien Rousé
2 days ago




Why would you want a shorter way to write it if you don't mind? Are you golfing or something?
– Julien Rousé
2 days ago












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
2 days ago




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
2 days ago












It's just that usually when people ask for the shortest way to write something it's for code golf
– Julien Rousé
2 days ago




It's just that usually when people ask for the shortest way to write something it's for code golf
– Julien Rousé
2 days ago




1




1




Ahh, never heard of that. No, I actually just try to learn it the 'right' way.
– Jon Programmer
2 days ago




Ahh, never heard of that. No, I actually just try to learn it the 'right' way.
– Jon Programmer
2 days ago












Keep asking question then ;) Next time you post here, copy a more sizeable snippet of code, feedbacks will be way more interesting.
– Julien Rousé
2 days ago




Keep asking question then ;) Next time you post here, copy a more sizeable snippet of code, feedbacks will be way more interesting.
– Julien Rousé
2 days ago










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
      2 days ago


















    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 2 days ago









        200_success

        127k15148412




        127k15148412
























            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
              2 days ago















            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
              2 days ago













            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 2 days ago

























            answered 2 days ago









            Julien Rousé

            659517




            659517








            • 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
              2 days ago














            • 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
              2 days ago








            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
            2 days ago




            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
            2 days ago



            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre