1

I am writing a program for my business that will require some division. For one of the user inputs, it is possible that 0 is a variable but it will then be required to be divided. If opps == 0 then sales and addon are irrelevant, but total might still have a variable assigned to it by the user. Line 9 contains my solution, but it requires the entering of false information. Is there any better way to handle this?

ans = 'y'

opps = []
sales = []
addon = []
total = []

while ans in ['y', 'Y', 'yes', 'Yes', 'YES']:
    opps.append(int(input("Number of opportunities: ")))
    while opps[-1] == 0:
        opps.append(int(input("Number of opportunities can not equal 0, input at least 1: ")))
    sales.append(int(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("\nResults: " + "\nAddon %: " + "%.2f" % addonp + "%\n" "CRA %: " + "%.2f" % cra + "%\n")

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

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

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

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

input("\nPress any key to close...")
  • 1
    There are several ways, search for divide by zero and you might find [questions like this](https://stackoverflow.com/questions/27317517/make-division-by-zero-equal-to-zero/28953601) which answer your question. – LinkBerest Jun 12 '18 at 14:10
  • 1
    Possible duplicate of [Make division by zero equal to zero](https://stackoverflow.com/questions/27317517/make-division-by-zero-equal-to-zero) – LinkBerest Jun 12 '18 at 14:11
  • you should look into `try: / except:` statement which is great to check user input. – Mathieu Jun 12 '18 at 14:18
  • @Mathieu I tried this, but it won't return my variables, either of you know why? https://hastebin.com/usowilired.py – Nick Cipriano Jun 12 '18 at 15:09
  • Looking at your code your problem is not in your try (though you really need to look at how Rick wrote that in his answer) but in your use of variables. Check out this question on [local vs. global scope](https://stackoverflow.com/questions/22439752/python-local-vs-global-variables) and the [python docs on the subject](https://docs.python.org/3/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value). As that code is trying to use a variable scoped to a function in the main code - and then not assigning it using = in the main code's function call. – LinkBerest Jun 12 '18 at 20:50

0 Answers0