0

Please check my code below, in that else condition matches (test data is "k"), but not executing the statement inside it, rather it gives error like -

NameError: name "k" is not defined.

at the same time if I give 8 or 9, it did not show the above error at the same time not executed the else statement as well, just showing the main menu again

from math import sqrt
def addition(firstNum,secondNum):
    total = firstNum+secondNum
    return total

def substraction(firstNum,secondNum):
    total = firstNum-secondNum
    return total

def multiplication(firstNum,secondNum):
    total = firstNum*secondNum
    return total

def division(firstNum,secondNum):
    total = firstNum/secondNum
    return total

def root(firstNum):
    total = (firstNum*firstNum)
    return total

menu={}
menu[" "] = "          --------PYTHON CALCULATOR-------            "
menu["."] = "~~Main Menu~~"
menu["1"] = "Addition"
menu["2"] = "Substraction"
menu["3"] = "Multiplication"
menu["4"] = "Division"
menu["5"] = "Square"
menu["6"] = "Root"
menu["7"] = "Exit\n"

while True:
    options = menu.keys()
    options.sort()
    for entry in options:
        print entry, menu[entry]

    selection = input("Please select your correct option: ")

    if selection   == 1:
        firstNum=input ("\nPlease enter first number : ")
        secondNum=input("Please enter Second number: ")

        print "Answer is " , addition(firstNum,secondNum)
        print "--------------------------------------------------------\n\n"
    elif selection == 2:
        firstNum=input ("\nPlease enter first number : ")
        secondNum=input("Please enter Second number: ")
        print "Answer is " ,substraction(firstNum,secondNum)
        print "--------------------------------------------------------\n\n"
    elif selection == 3:
        firstNum=input ("\nPlease enter first number : ")
        secondNum=input("Please enter Second number: ")
        print "Answer is ",multiplication(firstNum,secondNum)
        print "--------------------------------------------------------\n\n"
    elif selection == 4:
        firstNum=input ("\nPlease enter first number : ")
        secondNum=input("Please enter Second number: ")
        print "Answer is " ,division(firstNum,secondNum)
        print "--------------------------------------------------------\n\n"
    elif selection == 5:
        firstNum=input ("\nPlease enter first number : ")
        print "Answer is " ,sqrt(firstNum)
        print "--------------------------------------------------------\n\n"
    elif selection == 6:
        firstNum=input ("\nPlease enter first number : ")
        print "Answer is " ,root(firstNum)
        print "--------------------------------------------------------\n\n"
    elif selection == 7:
        break
    else:
        "Invalid option keyed in..."
MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
Prabhu
  • 17
  • 5
  • 1
    Use `raw_input` instead of `input`. But don't forget to convert the input to an int afterwards. – Aran-Fey Mar 06 '17 at 10:00
  • Please see http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response to see how to get valid input. – PM 2Ring Mar 06 '17 at 10:10

1 Answers1

1

Use raw_input instead of input. Here's why. raw_input return the string entered by the user, you should then convert to int using:

firstnum = int(raw_input('...'))
Community
  • 1
  • 1