首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的非活动变量在提价?

为什么我的非活动变量在提价?
EN

Stack Overflow用户
提问于 2019-10-28 23:17:45
回答 1查看 41关注 0票数 0

一个简单的程序,帮助我计算一些新地板的成本,但我的最终产出不是我所期望的。特别是,当底面为“否”时,底面面积的变量仍在取值,并在末尾打印。如果不是很明显,这是我第一次尝试。永远不会。

我期待‘边缘’和‘衬垫’的变量仍然是“否”,没有值将存储在那个that循环中。

代码语言:javascript
复制
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))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-28 23:32:05

您应该使用简单的if-statements,而不是在循环底部使用while-loop和“重置标志”。我还通过给变量指定名称来提高代码的可读性(永远不要给变量名称,如onetwo等)。您还必须定义edgeareaunderlayarea,因为如果用户至少在其中一个输入中输入"No",则会引发NameError

代码语言:javascript
复制
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")

我还想建议大家看一看枯燥的原则,也就是“不要重复自己”。这三种计算形式基本相同。这就是为什么为这些计算定义一个带有必要参数的函数的代码风格会更好。干法解决办法可能类似于以下几点:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58599559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档