stupidly easy hw that I need help with - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: stupidly easy hw that I need help with (/thread-4987.html) |
stupidly easy hw that I need help with - icantcodeforcrap - Sep-13-2017 I have to do a stupid tip calculator hw assignment for my beginning coding class. It's literally only our second assignment, but I'm stuck. For some reason, the program will not calculate the tip. I don't know what I'm doing wrong. It's probably something basic. Here's the input and output. Input----------------------------- tax = .06 # How much is the bill? bill = input("How much is the bill? ") # What percentage of the tip would you like to enter? tippy = input("What percentage of the tip would you like to enter? ") # Tip added print "Tip added is " + str(tippy) # What is the total bill? TotalBill = (bill * tax) + (bill * tippy) + bill print "Your total bill is " + str(TotalBill)Output---------------------------
Oh that's fancy. Sorry, literally just found this site, don't know much about it RE: stupidly easy hw that I need help with - nilamo - Sep-13-2017 Your output doesn't match your code. What I mean by that, is that based off the code you shared, the output you're getting is... not possible. How are you running it? Is it possible you're saving your script to one file, but running a different one? RE: stupidly easy hw that I need help with - icantcodeforcrap - Sep-13-2017 This is my window. I'm using cloud9. Now it's behaving differently. I don't know what made it change. I also altered my code, but I'm still getting the same final answer. tax = .06 # How much is the bill? bill = input("How much is the bill? ") # What percentage of the tip would you like to enter? tippy = input("What percentage of the tip would you like to enter? ") # Tip added print "Tip added is " + str(tippy) # What is the total bill? TotalBill = (bill * tax) + (bill * (tippy / 100)) + bill print "Your total bill is " + str(TotalBill)
RE: stupidly easy hw that I need help with - nilamo - Sep-13-2017 Ok, so that output at least matches your code. Here's what's in the image:
bill * tippy is 100 * 10 , which is 1000. Before you calculate the total, you should create a percentage based off the tip, maybe...tip_percent = tippy / 100.0 print(bill * tip_percent)Then use that, instead of tippy in the actual calculation. RE: stupidly easy hw that I need help with - nilamo - Sep-13-2017 Quote:
Because you're using python2, and because tippy is an int, if you do any calculations, you'll still have an int (the decimal part is ignored). So 10 / 100 is 0, not the 0.1 you expect. Instead, divide by 100.0, so python switches to a float, so the decimal value is kept.That's more complicated than it needs to be, and that's not how it works in python3. RE: stupidly easy hw that I need help with - icantcodeforcrap - Sep-13-2017 Ok, that worked. Thanks! |