一个简单的程序,帮助我计算一些新地板的成本,但我的最终产出不是我所期望的。特别是,当底面为“否”时,底面面积的变量仍在取值,并在末尾打印。如果不是很明显,这是我第一次尝试。永远不会。
我期待‘边缘’和‘衬垫’的变量仍然是“否”,没有值将存储在那个that循环中。
underlay='No'
edging=input('Are you ordering Edging?').title()
underlay=input('Are you ordering underlay?').title()
roomsize=input('How many square meters is the room?')
roomflt=float(roomsize)
while edging =='Yes':
#ask for user inputs
edgeprice=input("How much is the edging per meter?")
edgeperim=input('What is the perimeter of the room?')
#convert to float for calculation
one=float(edgeperim)
two=float(edgeprice)
#calculate
edgearea=one*two
#reset flag
edging='No'
while underlay=='Yes':
#ask for user input
underlayprice=input('How much per square meter for the Underlay?')
#convert to float for calculation
three=float(underlayprice)
four=float(roomflt)
#calculate
underlayarea=three*four
#reset flag
underlay='No'
#set the floor price
floorprice=input("How much is the floor per square meter?")
#convert to float for calculation
five=float(floorprice)
six=float(roomflt)
#calculate
area=five*six
#get the cost
addemup=(edgearea+underlayarea+area)
print("\n----------------------------------------------\nThe total is £{0:.2f} to purchase the flooring.".format(addemup))
print("This is made up of £{0:.2f} for the floor itself,".format(area))
print("This is made up of £{0:.2f} for the edging,".format(edgearea))
print("and £{0:.2f} for the underlay".format(underlayarea))发布于 2019-10-28 23:32:05
您应该使用简单的if-statements,而不是在循环底部使用while-loop和“重置标志”。我还通过给变量指定名称来提高代码的可读性(永远不要给变量名称,如one、two等)。您还必须定义edgearea和underlayarea,因为如果用户至少在其中一个输入中输入"No",则会引发NameError。
edgearea = 0
underlayarea = 0
edging = input('Are you ordering Edging?').title()
underlay = input('Are you ordering underlay?').title()
roomsize = input('How many square meters is the room?')
roomsize = float(roomsize)
if edging == 'Yes':
edgeprice = float(input("How much is the edging per meter?"))
edgeperim = float(input('What is the perimeter of the room?'))
edgearea = edgeperim * edgeprice
if underlay == 'Yes':
underlayprice = float(input('How much per square meter for the Underlay?'))
underlayarea = underlayprice * roomsize
floorprice = float(input("How much is the floor per square meter?"))
area = floorprice * roomsize
total_price = edgearea + underlayarea + area
print(f"\n----------------------------------------------\nThe total is {total} to purchase the flooring.")
print(f"This is made up of {area} for the floor itself,")
if edgearea:
print(f"This is made up of {edgearea} for the edging,")
if underlayarea:
print(f"and {underlayarea} for the underlay")我还想建议大家看一看枯燥的原则,也就是“不要重复自己”。这三种计算形式基本相同。这就是为什么为这些计算定义一个带有必要参数的函数的代码风格会更好。干法解决办法可能类似于以下几点:
def calculate(
name: str,
dimension: str,
unit: str,
mandatory: bool = True,
) -> float:
mandatory = mandatory or input(f"Do you order {name}?") == "Yes"
if mandatory:
relative_price = float(input(f"How much is the {name} per {unit}?"))
size = float(input(f"How much {dimension} is the room?"))
return size * relative_price
return 0
floor_price = calculate("floor", "area", "squaremeters")
edging_price = calculate("edging", "perimeter", "meters", False)
underlay_price = calculate("underlay", "area", "squaremeters", False)
total_price = floor_price + edging_price + underlay_pricehttps://stackoverflow.com/questions/58599559
复制相似问题