PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 14, 2022

[FIXED] How to create an if statement program to calculate a tip?

 November 14, 2022     error-handling, if-statement, python, python-2.7, python-3.x     No comments   

Issue

I'm trying to write a program for my homework:

Write a program to determine how much to tip the server in a restaurant. The tip should be 15% of the check, with a minimum of $2.

The hint said to use an if statement, but i keep running into a alot of errors.

I don't know how else to write it, I've been trying to figure it out for a couple of hours now and haven't found proper answers online...

Check = 59
Tip = (Check)*(0.15)

if Tip > 2: 
    print(Tip)

Solution

It's not clear whether only checks larger than $2 get tips, or whether tips must be at least $2.

I'm guessing the former, because you can do that with an if statement:

check = 59
if check > 2:
    tip = 0.15*check

Or more compactly, like this:

tip = check*0.15 if check > 2 else 0

Whereas if the intent is that the tip must be at least $2, an if statement isn't really necessary:

check = 59
tip = max(2, 0.15*check)

But if you HAD to use an if statement (though max is more elegant), you could do it like this:

check = 59
tip = 0.15*check
if tip <= 2:
    tip = 2

But this is quite inelegant.



Answered By - Vin
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing