2016-02-06 72 views
0

我想寫一個適合這些規則的程序:
你的租約已經到了,是時候搬家了。不幸的是,你有很多東西,你不想花太多時間來移動它,因爲你更願意練習你的編程技巧。謝天謝地,你們有可以幫助的朋友,雖然這種幫助是有代價的。你的朋友可以每小時移動20個盒子,但他們需要一個16英寸(直徑)的比薩餅來完成這項工作。用Python編寫一個函數,獲取您擁有的箱子數量,並返回您必須購買多少平方英尺的披薩。使用功能標題:sqFtPizza(numBoxes)我得到一個EOF錯誤,不知道如何解決它

並讓它將比薩的平方英尺作爲浮點數返回。

這是代碼我有

def sqFtPizza(numBoxes): 
    a = 3.14159*(8*8) 
    c = 1/12**2 
    sqft = a * c 
    za =numBoxes/20 
    area = za * sqft 
    print (area) 
def question(): 
    numBoxes= float(int(input("How many boxes do you have?: "))) 
    sqFtPizza(numBoxes) 
question() 

請幫助?

+0

,你想用'raw_input',而不是'input'。 – L3viathan

+2

你正在使用int的float嗎? –

+0

你可以使用'numBoxes = float(raw_input('多少個盒子?:'))' – tijko

回答

1

使得函數名PEP8兼容,

# --- Python 2.x --- 
from __future__ import division   # make int/int return float 
from math import pi 

PIZZA_PER_BOX = pi * (8/12)**2/20 # one 16" pizza per 20 boxes 

def sq_ft_pizza(num_boxes): 
    """ 
    Input: number of boxes to be moved 
    Output: square feet of pizza to feed movers 
    """ 
    return PIZZA_PER_BOX * num_boxes 

def main(): 
    num_boxes = float(raw_input("How many boxes must you move? ")) 
    print("You need {:0.2f} square feet of pizza!".format(sq_ft_pizza(num_boxes))) 

if __name__ == "__main__": 
    main() 

如果你在Python 2裏它運行像

How many boxes must you move? 120 
You need 8.38 square feet of pizza! 
相關問題