Reputation: 43
Is there a way I can shorten this code to perhaps use a loop that stores different variables each time it is run? I need it to store different variables rather than overwriting the same variable in the loop. As of right now they code is very lengthy and ends after five iterations, as coded, before displaying my end results. Any help would be much appreciated, thank you!
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
Upvotes: 1
Views: 307
Reputation: 4814
Try this:
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))
Explanation:
Simply put, you want to use lists
. They're used whenever you want to store multiple data values under one variable. In this case, there's no need to create new variables each time as you can simply append (add) to the list. The loop will run as long as ans
is in the whitelist. When it ends, your code for calculating the totals will run (note that sum()
can be used to calculate the sum of variables, or in this case, a list).
Upvotes: 1