有没有办法可以缩短这段代码,以便可以使用每次运行时存储不同变量的循环?我需要它来存储不同的变量,而不是覆盖循环中的相同变量。截至目前,他们的代码非常冗长,并且在显示我的最终结果之前按照编码进行了五次迭代后结束。任何帮助将不胜感激,谢谢!
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
最佳答案
试试这个:
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))
说明:
简单地说,您想使用列表
。每当您想要在一个变量下存储多个数据值时,就会使用它们。在这种情况下,无需每次都创建新变量,因为您可以简单地追加(添加)到列表中。只要 ans
在白名单中,循环就会运行。当它结束时,用于计算总计的代码将运行(请注意,sum()
可用于计算变量的总和,在本例中为列表)。
关于python - 每次迭代存储不同变量的循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50724788/