Im a beginner to python and my professor has assigned the following assignment:

You buy an international calling card to India. The calling card company has some
special offers.
a) If you charge your card with $5 or $10, you don’t get anything extra
b) For a $25 charge, you get $3 of extra phone time.
c) For a $50 charge, you get $8 of extra phone time
d)For a $100 charge, you get $20 extra phone time.
Write a function that asks the user for the amount he/she wants on the card, and returns
the total charge that the user gets. Note: Values other than those mentioned above are
not allowed.
Given: The problem as stated.
When designing the function, only think about the values of 5, 10, 25, 50, and 100
An additional rule: The else on the if-elif-else sequence will return the input value.
Find:
Create a function that takes no parameter, but does prompt the user for an input.
Convert that input to a number, and use a series of if-elif-else statements to find the
value of the card.
The new total value according to rules a,b,c,d is returned from the function.
Call the function from the menu and assign the return value to a variable.
Print out that variable.

I have written a function doing the following, but when I run it I cant get any of the options past a to run. What am I missing? Any help will be appreciated.

def callcard ():
    print "A. For $5 or $10."
    print "B. For a $25 charge, you get $3 of extra phone time."
    print "C. For a $50 charge, you get $8 of extra phone time."
    print "D. For a $100 charge, you get $20 extra phone time."
    print " "
    print " "
    IOU = raw_input ("Please enter the letter for the amount you wish to enter: ")
    print " "
    
    loop=True
    while (loop):
        if (IOU == 'a' or 'A'):
            print "You have chosen to charge $5 or $10."
            amount = raw_input ("Please enter '5' for $5 or '10' for $10: ")
            if (amount == '5'):
                print "You have charged $5, thank you for your purchase."
            elif (amount == '10'):
                print "You have charged $10, thank you for your purchase."
            else:
                print "Incorrect value"
        elif (IOU == 'b' or 'B'):
            print "You have chosen to charge $25, you get $3 of extra phone time"
            print " "
            amount2 = raw_input ("Please confirm your purchase. Press 'Y' for yes or 'N' for no: ")
            if (amount2 == 'Y' or 'y'):
                print "You now have $28 of phone time, thank you for your purchase!"
                print" "
            elif (amount2 == 'N' or 'n'):
                print "Thank you!"
                print " "
            else:
                print "Invalid option, please try again."
        elif (IOU == 'c' or 'C'):
            print "You have chosen to charge $50, you will receive $8 of extra phone time."
            print " "
            amount3 = raw_input ("Please confirm your purchase. Press 'Y' for yes or 'N' for no: ")
            if (amount3 == 'Y' or 'y'):
                print "You now have $58 of phone time, thank you for your purchase!"
                print " "
            elif (amount3 == 'N' or 'n'):
                print "Thank you!"
                print " "
            else:
                print "Invalid option, please try again."
        elif (IOU == 'd' or 'D'):
            print "You have chosen to charge $100, you will receive $20 of extra phone time."
            print " "
            amount4 = raw_input ("Please confirm your purchase. Press 'Y' for yes or 'N' for no: ")
            if (amount4 == 'Y' or 'y'):
                print "You now have a total of $120 of phone time, thank you for your purchase!"
                print" "
            elif (amount4 == 'N' or 'n'):
                print "Thank you!"
                print " "
            else:
                print "Invalid option, please try again."
        else:
            print "Invalid Option"
            loop=False
    else:
        raw_input("Done")
                
callcard()

Recommended Answers

All 4 Replies

All your conditions are allways True as second part of or is not zero/False. Maybe you want case insensitive input? If so, use lower method to input and check only for small letters. BTW you where instructed to take numeric input which is in allowed values, not letters.

All your conditions are allways True as second part of or is not zero/False. Maybe you want case insensitive input? If so, use lower method to input and check only for small letters. BTW you where instructed to take numeric input which is in allowed values, not letters.

Awesome, your a life saver. Ive changed it up with using numbers and it works great, but I do have one question. How do I get the program to loop back to the main menu after the user has charged the amount requested?

Heres the new code:

def callcard ():
    print "1. For $5 or $10."
    print "2. For a $25 charge, you get $3 of extra phone time."
    print "3. For a $50 charge, you get $8 of extra phone time."
    print "4. For a $100 charge, you get $20 extra phone time."
    print " "
    print " "
    IOU = raw_input ("Please enter the number for the amount you wish to charge: ")
    print " "
    
    loop=True
    while (loop):
        if (IOU == '1'):
            print "You have chosen to charge $5 or $10."
            amount = raw_input ("Please enter '5' for $5 or '10' for $10: ")
            if (amount == '5'):
                print "You have charged $5, thank you for your purchase."
            elif (amount == '10'):
                print "You have charged $10, thank you for your purchase."
            else:
                print "Incorrect value"
        elif (IOU == '2'):
            print "You have chosen to charge $25, you get $3 of extra phone time"
            print " "
            amount2 = raw_input ("Please confirm your purchase. Press '1' for yes or '2' for no: ")
            if (amount2 == '1'):
                print "You now have $28 of phone time, thank you for your purchase!"
                print" "
            elif (amount2 == '2'):
                print "Thank you!"
                print " "
            else:
                print "Invalid option, please try again."
        elif (IOU == '3'):
            print "You have chosen to charge $50, you will receive $8 of extra phone time."
            print " "
            amount3 = raw_input ("Please confirm your purchase. Press '1' for yes or '2' for no: ")
            if (amount3 == '1'):
                print "You now have $58 of phone time, thank you for your purchase!"
                print " "
            elif (amount3 == '2'):
                print "Thank you!"
                print " "
            else:
                print "Invalid option, please try again."
        elif (IOU == '4'):
            print "You have chosen to charge $100, you will receive $20 of extra phone time."
            print " "
            amount4 = raw_input ("Please confirm your purchase. Press '1' for yes or '2' for no: ")
            if (amount4 == '1'):
                print "You now have a total of $120 of phone time, thank you for your purchase!"
                print" "
            elif (amount4 == '2'):
                print "Thank you!"
                print " "
            else:
                print "Invalid option, please try again."
        else:
            print "Invalid Option"
            loop=False
    else:
        raw_input("Done")
                
callcard()

Use a return from the function (untested code).

def callcard ():
    IOU="0"
    while IOU not in ["1", "2", "3", "4"]:
        print
        print "1. For $5 or $10."
        print "2. For a $25 charge, you get $3 of extra phone time."
        print "3. For a $50 charge, you get $8 of extra phone time."
        print "4. For a $100 charge, you get $20 extra phone time."
        print " "
        print " "
        IOU = raw_input ("Please enter the number for the amount you wish to charge: ")
    print " "
 
    loop=True
    while (loop):    ## not necessary as the above guarantees correct input
        if (IOU == '1'):
            amount = "0"
            while amount not in ["5", "10"]:
                print "You have chosen to charge $5 or $10."
                amount = raw_input ("Please enter '5' for $5 or '10' for $10: ")
            if (amount == '5'):
                print "You have charged $5, thank you for your purchase."
            elif (amount == '10'):
                print "You have charged $10, thank you for your purchase."
#            else:
#                print "Incorrect value"
            return  ## exit function 
#
# etc for the other options

again="Y"
while again == "Y":
    callcard()
    print  
    again = raw_input("Enter 'Y' for a transaction;  any other key to exit ")
    again = again.upper()

Since options 2, 3, & 4 are all the same except for the amount, you can pass the necessary parameters to a function, and use it for any of those three options.

commented: Awesome +0

Use a return from the function (untested code).

def callcard ():
    IOU="0"
    while IOU not in ["1", "2", "3", "4"]:
        print
        print "1. For $5 or $10."
        print "2. For a $25 charge, you get $3 of extra phone time."
        print "3. For a $50 charge, you get $8 of extra phone time."
        print "4. For a $100 charge, you get $20 extra phone time."
        print " "
        print " "
        IOU = raw_input ("Please enter the number for the amount you wish to charge: ")
    print " "
 
    loop=True
    while (loop):    ## not necessary as the above guarantees correct input
        if (IOU == '1'):
            amount = "0"
            while amount not in ["5", "10"]:
                print "You have chosen to charge $5 or $10."
                amount = raw_input ("Please enter '5' for $5 or '10' for $10: ")
            if (amount == '5'):
                print "You have charged $5, thank you for your purchase."
            elif (amount == '10'):
                print "You have charged $10, thank you for your purchase."
#            else:
#                print "Incorrect value"
            return  ## exit function 
#
# etc for the other options

again="Y"
while again == "Y":
    callcard()
    print  
    again = raw_input("Enter 'Y' for a transaction;  any other key to exit ")
    again = again.upper()

Since options 2, 3, & 4 are all the same except for the amount, you can pass the necessary parameters to a function, and use it for any of those three options.

Simply amazing. I hadn't even thought of using options 2-4 as another function. Awesome, I had already changed up the code since my last post and now Ill change it a bit thanks to your suggestions.

Thanks a ton :icon_smile:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.