首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每个迭代都存储不同变量的循环?

每个迭代都存储不同变量的循环?
EN

Stack Overflow用户
提问于 2018-06-06 15:59:41
回答 1查看 115关注 0票数 1

是否有办法缩短这段代码,以便在每次运行时使用一个存储不同变量的循环?我需要它存储不同的变量,而不是在循环中覆盖相同的变量。到目前为止,它们的代码非常长,在显示我的最终结果之前,经过五次迭代(按编码)结束。任何帮助都将不胜感激,谢谢!

代码语言:javascript
复制
while True:

opps = float(input("Number of opportunities: "))

sales = float(input("Quantity of of sales: "))

addon = float(input("Addon $ amount: "))

total = float(input("Sales dollar amount: "))

cra =  (sales / opps) * 100 

addonp = (addon / total) * 100

print("Results:\n" +
"CRA %: " + str(cra) + "%\n" +
"Addon %: " + str(addonp) + "%\n")

ans = 'y' 

ans = str(input("Continue? (Y/N)"))

if ans in ['y', 'Y', 'yes', 'Yes', 'YES']:

    opps2 = float(input("Number of opportunities: "))

    sales2 = float(input("Quantity of of sales: "))

    addon2 = float(input("Addon $ amount: "))

    total2 = float(input("Sales dollar amount: "))

    cra2 =  (sales2 / opps2) * 100 

    addonp2 = (addon2 / total2) * 100

    print("Results:\n" +
    "CRA %: " + str(cra2) + "%\n" +
    "Addon %: " + str(addonp2) + "%\n")

    ans = 'y' 

    ans = str(input("Continue? (Y/N)"))

if ans in ['y', 'Y', 'yes', 'Yes', 'YES']:

    opps3 = float(input("Number of opportunities: "))

    sales3 = float(input("Quantity of of sales: "))

    addon3 = float(input("Addon $ amount: "))

    total3 = float(input("Sales dollar amount: "))

    cra3 =  (sales3 / opps3) * 100 

    addonp3 = (addon3 / total3) * 100

    print("Results:\n" +
    "CRA %: " + str(cra3) + "%\n" +
    "Addon %: " + str(addonp3) + "%\n")

    ans = 'y' 

    ans = str(input("Continue? (Y/N)"))

if ans in ['y', 'Y', 'yes', 'Yes', 'YES']:

    opps4 = float(input("Number of opportunities: "))

    sales4 = float(input("Quantity of of sales: "))

    addon4 = float(input("Addon $ amount: "))

    total4 = float(input("Sales dollar amount: "))

    cra4 =  (sales4 / opps4) * 100 

    addonp4 = (addon4 / total4) * 100

    print("Results:\n" +
    "CRA %: " + str(cra4) + "%\n" +
    "Addon %: " + str(addonp4) + "%\n")

    ans = 'y' 

    ans = str(input("Continue? (Y/N)"))

if ans in ['y', 'Y', 'yes', 'Yes', 'YES']:

    opps5 = float(input("Number of opportunities: "))

    sales5 = float(input("Quantity of of sales: "))

    addon5 = float(input("Addon $ amount: "))

    total5 = float(input("Sales dollar amount: "))

    cra5 =  (sales5 / opps5) * 100 

    addonp5 = (addon5 / total5) * 100

    print("Results:\n" +
    "CRA %: " + str(cra5) + "%\n" +
    "Addon %: " + str(addonp5) + "%\n")

    ans = 'y' 

    ans = str(input("Continue? (Y/N)"))


if ans not in ['y', 'Y', 'yes', 'Yes', 'YES']:

    oppst = opps + opps2 + opps3 + opps4 + opps5

    salest = sales + sales2 + sales3 + sales4 + sales5

    addont = addon + addon2 + addon3 + addon4 + addon5

    cratp = (salest / oppst) * 100

    tsales = total + total2 + total3 + total4 + total5

    addontp = (addont / tsales) * 100

    int(oppst)
    int(salest)

    print("Your totals are: \n" + 
"\n" +
"Opportunities: " + str(int(oppst)) + "\n" +
"\n" +
"# of Sales: " + str(int(salest)) + "\n" +
"\n" +
"Addon $ amount: " + "$" + str(addont) + "\n" +
"\n" +
"Addon %: " + str(addontp) + "%\n" +
"\n" +
"CRA %: " +  str(cratp) + "%\n" +
"\n" +
"Total Sales: " + "$" + str(tsales)
        )
    break
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-06 16:08:00

试试这个:

代码语言:javascript
复制
ans = 'y'
opps = []
sales = []
addon = []
while ans not in ['y', 'Y', 'yes', 'Yes', 'YES']:
    opps.append(float(input("Number of opportunities: ")))
    sales.append(float(input("Quantity of of sales: ")))
    addon.append(float(input("Addon $ amount: ")))
    total.append(float(input("Sales dollar amount: ")))

    cra =  (sales[-1] / opps[-1]) * 100
    addonp = (addon[-1] / total[-1]) * 100
    print("Results:\n" + "CRA %: " + str(cra) + "%\n" +"Addon %: " + str(addonp) + "%\n")

    ans = str(input("Continue? (Y/N)"))

oppst = sum(opps)
salest = sum(sales)
addont = sum(addon)
cratp = (salest / oppst) * 100
tsales = sum(sales)
addontp = (addont / tsales) * 100

print("Your totals are: \n" + "\n" + "Opportunities: " + str(int(oppst)) + "\n" + "\n" + "# of Sales: " + str(int(salest)) + "\n" + "\n" + "Addon $ amount: " + "$" + str(addont) + "\n" + "\n" + "Addon %: " + str(addontp) + "%\n" + "\n" + "CRA %: " +  str(cratp) + "%\n" + "\n" + "Total Sales: " + "$" + str(tsales))

解释:

简单地说,您想要使用lists。每当您想要在一个变量下存储多个数据值时,都会使用它们。在这种情况下,不需要每次创建新变量,因为您可以简单地将(add)追加到列表中。只要ans在白名单中,循环就会运行。当它结束时,计算总计的代码将运行(请注意,sum()可以用于计算变量之和,或者在本例中是一个列表)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50724788

复制
相关文章

相似问题

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