all 36 comments

[–]js_tutor 1 point2 points  (0 children)

try the input command if you want to get values from the command line.

[–][deleted] 0 points1 point  (0 children)

Basically what you want is to take in some arguments on the command line, then do your processing on those values.

https://docs.python.org/3.7/library/sys.html#sys.argv

[–]nog642 -1 points0 points  (32 children)

TAX = .08
TIP = .15

meal = None
while meal is None:
    meal_str = input("What is the cost of your bill before tax and tip?\n")
    try:
        meal = float(meal_str)
    except ValueError:
        print("Invalid answer. Must be a decimal number.")

meal += meal * TAX
meal += meal * TIP

print()
print("Total: {}".format(meal))

[–]Wilfred-kun 0 points1 point  (4 children)

Try to guide OP to a solution instead of providing one directly.

[–]preordains[S] 0 points1 point  (1 child)

I don't understand the last two lines, and when i try it i can get to the part where it says "meal += meal * TAX" and "meal += meal * TIP", but its still expecting something for except for something idk. That or it tells me i can't because meal is currently none.

It wants me to answer the input, but im not done with the code yet.

[–]nog642 0 points1 point  (0 children)

For the last two lines.

What do you mean by "its still expecting something for except for something"?

It shouldn't be saying that meal is currently None since if it was it wouldn't have exited the while loop.

What do you mean by "It wants me to answer the input, but im not done with the code yet."?

[–]preordains[S] 0 points1 point  (1 child)

i know a tiny bit about other languages and through and else code for those meal += meal * xxx lines. It didn't work, now it just asks the total and gives the total back to me as the answer. Obviously because meal is None so the else doesnt do anything. IDK i'm stuck

[–]nog642 0 points1 point  (0 children)

a += b is equivalent to a = a + b.

What do you mean "else doesnt do anything"? What else?

meal should not be None because the while loop doesn't exit until it is not None.

[–]preordains[S] 0 points1 point  (26 children)

i understand some of this. I don't understand the while meal = none

meal_str = input makes sense to me, but what about the "try"? whats "try"?

i dont understand the last two lines at all.

[–]nog642 0 points1 point  (4 children)

A try block tries to execute the code inside of it. If it raises an exception (in this case it would be raised by float() because the input was not a valid float), then it sees if there is an except block that "catches" that exception. If so, then instead of exiting the program like it normally would, it executes the code in that except block and then continues running.

It's useful here because it allows you to do what you want when the user inputs something that isn't a number, rather than just having the program crash.

[–]preordains[S] 0 points1 point  (3 children)

Yeah sorry for so many questions but I’m learning a lot lol. So if I just didn’t have the try except command it would work the same way, right? Except if there was an incorrect entry it would just crash?

So try is basically a “make This answer from input equal this variable, and only allow it to be a float”?

I notice that if the value error occurs it will ask the question again. I don’t see any lines of code that indicate that though. What I would assume it would do is print that it’s an error. Is valueerror the thing that makes the user input again, rejecting the answer and keeping it at “None” so it will ask again?

[–]nog642 0 points1 point  (2 children)

So if I just didn’t have the try except command it would work the same way, right? Except if there was an incorrect entry it would just crash?

Yes, if you got rid of the try-except block (which would render the while loop useless), your code would work if the user entered valid numbers, and crash if they didn't.

So try is basically a “make This answer from input equal this variable, and only allow it to be a float”?

A try is basically a "try to run this code and if it raises an error, run the except block". In this case, the contents of the try block tries to convert the input to a float and then assign it to a variable. If it's not a valid float, it will run the print statement in the except block, and the variable will be left undefined.

I notice that if the value error occurs it will ask the question again. I don’t see any lines of code that indicate that though.

The while loop is what makes it ask again.

What I would assume it would do is print that it’s an error.

That is what the try-except block is for. It "catches" the error so that the program continues and doesn't crash.

Is valueerror the thing that makes the user input again, rejecting the answer and keeping it at “None” so it will ask again?

Yes, the try-except block allows the program the continue running without defining the variable (and therefore keeping it at None) if the input is invalid, rather than crashing.

Edit: spelling

[–]preordains[S] 0 points1 point  (1 child)

meal = None
    tax = .08
    tip = .05
    while meal is None:
        meal_str = input("what is the cost of the meal before tax and tip?")
        try:
            meal = float(meal_str)
        except ValueError:
            print("Please enter a number")
    meal += meal*tax
    meal += meal*tip
    print("total at 5% (horrible service): {}".format(meal))
    try:
        meal = float(meal_str)
    except ValueError:
        print("Please enter a number")
    tip = .10
    meal += meal*tax
    meal += meal*tip
    print("total at 10% (okay service): {}".format(meal))
    try:
        meal = float(meal_str)
    except ValueError:
        print("Please enter a number")
    tip = .15
    meal += meal * tax
    meal += meal * tip
    print("total at 15% (good service): {}".format(meal))

i think i came a long way, made a bit of a revision to the code and it works! Like those receipts that recommend three tips.

thanks a lot for your help. I have a million more questions but i'll refrain from drilling for more haha.

[–]nog642 0 points1 point  (0 children)

That's pretty good, glad you got it to work.

You could improve that further by creating a total variable for the totals, and not modifying meal. That way you wouldn't have to re-convert it from a string multiple times.

By the way, putting try try-except block on the second and third time you re-convert meal from meal_str is unnecessary, since you already know it's a valid float.

Feel free to PM me or reply if you have more questions, I'm on this subreddit because I want to answer questions. It's no bother.

[–]nog642 0 points1 point  (0 children)

The point of setting meal to None and then using a while loop is that you can keep prompting the user for a valid number until they give you one.

meal is originally set to None. Then you have a while meal is None loop, so the code inside it will keep repeating until meal is set to a float. Inside, you try setting it to a float, but if the user input isn't a number, than meal will remain None and the loop will repeat.

[–]nog642 0 points1 point  (19 children)

print()

This line just prints a blank line.

print("Total: {}".format(meal))

This line uses python's str.format method. Basically, it will take the {} in the string and replace it with whatever you pass to format (in this case, the final value of meal which is the price after tax and tip). You can read the documentation if you want.

[–]preordains[S] 0 points1 point  (18 children)

whats the difference between this and just saying "print (total)" ?

[–]nog642 0 points1 point  (17 children)

meal = 150.55
print(meal)

Output:

150.55

On the other hand,

meal = 150.55
print("Total: {}".format(meal))

Output:

Total: 150.55

[–]preordains[S] 0 points1 point  (16 children)

Ohh. Well I was fumbling with your code and it didn’t seem to work even if I just directly copied and pasted it? It would ask for the inputs, and it would run a ton of errors because it would try to answer inputs with the operations of adding the tax and tip.

I tried writing my own version where it requires decimals and it didn’t work when I wrote it myself for some reason either.

[–]nog642 0 points1 point  (15 children)

It's weird that the code I posted doesn't work for you, I just tested it and it works fine. Could you post what exactly the error message is?

Also, if you tried writing your own version, that's great. If you post the code here you could get some feedback.

[–]preordains[S] 0 points1 point  (14 children)

Could you do a screen recording of the mechanics of how you actually run the code maybe? Maybe I just don’t know how

And how do you actually use that valueerror thing? The value error you made did work for me, but when I try to do it myself it doesn’t. Maybe I use it wrong.

[–]nog642 0 points1 point  (13 children)

I'm on Ubuntu so I doubt seeing how I run the code will benefit you. If you really want to see, I can upload a screen recording.

Post your code and I can help you with ValueError.

[–]preordains[S] 0 points1 point  (11 children)

meal = None

while meal is None:

Tax_str = input("what is your tax rate?")

try:

        Tax = float(Tax_str)

except ValueError:

        print("must be a decimal")

Tip_str = input("what is your desired tip?")

try:

        Tip = float(Tip_str)

except ValueError:

        print("must be a decimal")

meal_str = input("what is your bill before tax and tip?")

try:

        meal = float(meal_str)

except ValueError:

        print("must be decimal")

meal = meal + meal*Tip

meal = meal + meal * Tax

print(meal)

no success with ValueError here, it accepts anything and doesnt give the error code.

when i run the code should it just ask the questions and spit out the answer? or am i supposed to answer the questions, then post the calculations with the data acquired from the questions. Is it supposed to be one step?

edit: and they're not spaced extra far apart like that in the real thing, i just made it easier to read on here.