为什么我的非活动变量会涨价



一个简单的程序,可以帮助我计算一些新地板的成本,但我的最终输出并不是我所期望的。特别是,当参考底图为"否"时,参考底图区域的变量仍在拾取值并在末尾打印。如果它不是显而易见的,这是我第一次尝试。

我预计,当"edge"one_answers"underline"的变量保持为"No"时,while循环中不会存储任何值。

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))

您应该使用简单的if-语句,而不是使用while-循环和在循环底部"重置标志"。我还通过给变量命名来提高代码的可读性(never给变量命名,如onetwo等等(。您还必须定义edgeareaunderlayarea,因为如果用户在至少一个输入中输入"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")

我还建议大家看看DRY原则,也就是"不要重复自己"。这三种计算的形式基本相同。这就是为什么为这些计算定义一个函数会更好的代码风格,该函数需要必要的参数。DRY解决方案可能类似于以下内容:

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_price

最新更新