2

I need this program to do one of two things from my repeat function, but it should be able to do both and I can't work out why. It must do 2 things

  • make it rerun my main() subprogram,
  • or say "Goodbye." and close the entire program, whichever one comes first.

I have tried making it just an if-else statement rather than if-elif-else, which didn't change anything, I have also tried rearranging the code, but that only changes the single output I can get from the subprogram. This is the subprogram currently:

def repeatloop():
  repeat = input("Do you want to calculate another bill? (y/n): ")
if repeat == 'n' or 'N':
  print("Goodbye.")
  time.sleep(2)
  sys.exit()
elif repeat == 'y' or 'Y':
  main()
else:
  print("Error. Program will shut down.")
  time.sleep(2)
  sys.exit()

It should be able to repeat the program (based on the y or Y input), terminate the program and display "Goodbye." based on the n or N input or it should display the "Error. Program will shut down." message before closing if an invalid input is entered. Many thanks to whoever can help me!

Neeraj
  • 1,769
  • 3
  • 24
  • 41
Sweaney
  • 23
  • 2
  • Is your indentation correct? – rdas Apr 17 '19 at 03:57
  • Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) Also see https://stackoverflow.com/q/20002503 – iz_ Apr 17 '19 at 04:21

3 Answers3

0

Hello and welcome to Stackoverflow.
Your problem is that your if statements are not exactly correct; and also your whole function is not correctly indented.

if repeat == 'n' or 'N' is different from if repeat == 'n' or repeat == 'N'.

In the second case, you test two different statements on the repeat keyword, where on the first one you're seeing whether:

  1. repeat is equal to n, and also
  2. 'N' is not None; which isn't & this case always returns true.

Another way to do it could be if repeat in ["n", "N"] or even better: if repeat.lower() == 'n'

So put it all together, your code should be refined to:

def repeatloop():
    repeat = input("Do you want to calculate another bill? (y/n):")
    if repeat.lower() == 'n':
        print("Goodbye.")
        time.sleep(2)
        sys.exit()
    elif repeat.lower() == 'y':
        main()
    else:
        print("Error. Program will shuts down.")
        time.sleep(2)
        sys.exit()
ggrelet
  • 1,071
  • 7
  • 22
  • 1
    `any(["n", "N"]) == repeat` won't give you the right answer either. You would be getting `True == repeat` which is always `False`. – iz_ Apr 17 '19 at 04:22
0

You may use in () for the comparison with a list. It compares the value of the variable with the list.

def repeatloop():
  repeat = input("Do you want to calculate another bill? (y/n): ")
  if repeat in ('n','N'):
    print("Goodbye.")
    time.sleep(2)
    sys.exit()
  elif repeat in ('y','Y'):
    main()
  else:
    print("Error. Program will shut down.")
    time.sleep(2)
    sys.exit()
Neeraj
  • 1,769
  • 3
  • 24
  • 41
0

if repeat == 'n' or 'N' doesn't work, since you are checking bool('N') which is always true, the same holds true for repeat == 'y' or 'Y', since bool('Y') is always true.

You start by checking if repeat is in the list ['n','N'], or is not in the list ['y','Y'], then you exit the program, else you call the same function repeatloop

import time, sys

def repeatloop():
    repeat = input("Do you want to calculate another bill? (y/Y/n/N): ")
    if repeat in ['n', 'N']:
        print("Goodbye.")
        time.sleep(2)
        sys.exit()
    elif repeat in ['y', 'Y']:
       repeatloop()
    else:
        print("Error. Program will shut down.")
        time.sleep(2)
        sys.exit()

repeatloop()
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40