0

So I have a code that project the user's bill. I put it on a loop so that it keeps repeating itself until the user don't want to continue.

here is my code:

status = True

def kill() :
    confirm = input("Again? (Y/N) : ")
    if confirm == "n":
        status = False

while(status):
    plan_option = input("Which plan are using ? (a/b/c/d/e):   ").lower()
    if plan_option == "a" :
        print("Your current bill is : $90")
        kill() 
    else :
        data_amount = float(input("How much data have you used?          "))

print("===========================") 

def plan_b(x) :
    if x < 10 :
        print("Your current bill is : $60")
    elif x > 10 :
        total = 60 + x*10
        print("Your current bill is : $", total)

def plan_c(x) :
    if x < 5 :
        print("Your current bill is : $40")
    elif x > 5 :
        total = 40 + x*12
        print("Your current bill is : $", total)

def plan_d(x) :
    if x < 2 :
        print("Your current bill is : $30")
    elif x > 2 :
        total =  + x*15
        print("Your current bill is : $", total)

def plan_e(x) :
        total = x*18
        print("Your current bill is : $", total)


if plan_option == "b" :
    plan_b(data_amount)
elif plan_option == "c" :
    plan_c(data_amount)
elif plan_option == "d" :
    plan_d(data_amount)
elif plan_option == "e" :
    plan_e(data_amount)

kill()

So my questions are :

  1. If I enter "n" when the code prompt me, the script won't stop and kept going back to plan_option.
  2. even though the code stops (eventually), it kept prompting me "Again? (Y/N) : " before it kills itself.

Where did I do wrong? Also, am I over-engineering here?

2
  • Related: stackoverflow.com/questions/291978/… Commented Jul 1, 2016 at 7:09
  • 2
    Rather than use a global for status, it would be better if kill() returned a status value. Commented Jul 1, 2016 at 7:10

4 Answers 4

6

You have to declare 'status' as a global variable inorder to update value to
"status = False" in your kill method.

You can do 2 things here:
1. Declare status as global variable
2. Return "status" (which is a local variable) from kill method

You can check the tutorials on how to use global variables. Am not going to provide you the code (for your own good ofcourse).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! It stops now, but even though the loop stops, it promps "Again? (Y/N) : " one more time before it kills itself. Why is that?
@YukaLangbuana That wasn't your original question. Please keep your question focused on a particular problem.
@YukaLangbuana that could be because you are calling "kill" again in the last line
3
def kill() :
    confirm = input("Again? (Y/N) : ")
    if confirm == "n":
        status = False

This creates a local variable named status, and sets that. The global variable of the same name is unaffected.

Add global status in the function so that it uses the global one instead:

def kill() :
    global status
    confirm = input("Again? (Y/N) : ")
    if confirm == "n":
        status = False

2 Comments

Thanks! It stops now, but even though the loop stops, it promps "Again? (Y/N) : " one more time before it kills itself. Why is that?
Because the last line of your program is to call kill() one more time.
1

I think that you should use less complicated way:

try this model

===>here your function a()<===

def a():
    print("A plan in action")


while True:
    ===> your loop code <===


    your_input = input("> ")
    your_input = your_input.upper()


    if your_input == 'N':
        print("\n** You escaped! **\n")
        break
    elif your_input == "A":
        print("\n** Plan A lunched ! **\n")
        a()
        ===>you can use 'break' to stop the loop here <=== 
    else:
        continue

Comments

0

Two revisions:

  1. Use global status in kill()
  2. If you are comparing confirm == "n", then convert n to lower while taking input

Try this:

def kill() :
    confirm = input("Again? (Y/N) : ").lower()
    if confirm == "n":
        global status
        status = False

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.