Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,637 Points

Assigning float variable value based on yes-no user input

The assignment is to calculate pie cost per square inch given a size and price. I am adding a user-interactive dimension (so, I am improvising for my own edification) to ask the user for size and whether s/he wants toppings. I cannot figure out how to get the yes or no on toppings from the user and add the surcharge. It ignores the response to the question calle by that method call and just gives the price of the pie whether toppings are requested or not.

What is wrong with my code? I have tried a lot of things. I must be missing something small and obvious.

import math

#pie price per square inch (the actual assignment)
def piecost(diameter, price):
    radius = (diameter / 2)
    area = (math.pow(radius, 2)) * math.pi

    return round(price / area, 2)
    #return price / area

#a menu to show the user (I am going to improve on this - right now the user puts in the #diameter size and not a semantic response.
def showprice():
    print("8 inch personal is 6.50")
    print("10 inch small is 10.00")
    print("12 inch medium is 11.50")
    print("14 inch large is 15.50")
    print("16 inch extra large is 18.00")

#the function that adds the 2.00 charge for toppings. Except it doesn't
def toppings(answer):
    baseprice = 0.0
    if answer.lower == 'y':
        baseprice = 2.00

    return baseprice


#price of pizza with or without toppings, as user prefers. Only gives prices without.
def pricecalc(answer, diameter):
    baseprice = toppings(answer)
    price = 0.0
    price = price + baseprice
    if diameter == 8:
        price = price + 6.50
    if diameter == 10:
        price = price +  10.00
    if diameter == 12:
        price = price + 11.50
    if diameter == 14:
        price = price + 15.50
    if diameter == 16:
        price = price + 18.00

    return price

#main and method calls
def main():

    showprice()

    diameter = eval(input("What size pizza pie do you want? "))
    answer = input("Do you want toppings Y/N? ")
    baseprice = toppings(answer)
    pieprice = pricecalc(answer, diameter)
    totalcost = piecost(diameter, pieprice)
    print(str(baseprice))
    print(str(pieprice))
    print(str(totalcost))

    print("Your ", str(diameter), " inch pizza", " is ", str(pieprice), "which is ", str(totalcost), " cents per square inch.")
    print("Goodbye")


main()

2 Answers

In function toppings, lower() is a function.

def toppings(answer):
    baseprice = 0.0
    if answer.lower() == 'y':
        baseprice = 2.00
    return baseprice

let me know if you have any other problem.

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,637 Points

I will plug in when I am at the computer with the program on it and let you know. Thanks for taking the time to answer.