Python Forum
stupidly easy hw that I need help with
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
stupidly easy hw that I need help with
#1
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---------------------------
Output:
How much is the bill? 100 What percentage of the tip would you like to enter? 10 Tip added is 0 Your total bill is 106.0

Oh that's fancy. Sorry, literally just found this site, don't know much about it
Reply
#2
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?
Reply
#3
This is my window. I'm using cloud9.
[Image: kB7e1v]

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) 
Output:
How much is the bill? 100 What percentage of the tip would you like to enter? 10 Tip added is 10 Your total bill is 106.0
Reply
#4
Ok, so that output at least matches your code.

Here's what's in the image:
Output:
How much is the bill? 100 What percentage of the tip would you like to enter? 10 Tip added is 10 Your total bill is 1106.0
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.
Reply
#5
Quote:(tippy/100) 
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.
Reply
#6
Ok, that worked. Thanks!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020