简体   繁体   中英

Input number A and B and sum them together, given that A has a pre-defined limit in Python

I am doing a calculator that input number A and number B then sum them up, given that A has a pre-defined limit (eg A has to be less than 8)

def welcome():
    print("Welcome user")
    print("")

def ans_num1():
    num1 = int(input("Enter your 1st num: "))
    while num1 <= limit1:
        print("Good boy")
        break
    else:
        print("Wrong input")
        ans_num1()

def ans_num2():
    num2 = input("Enter your 2st num: ")


def calculator():   
    print("The sum of 2 numbers are: ")
    print(num1 + num2)
    print("")

def thanks():
    print("Thank you and Goodbye :)")


welcome()
limit1 = int(input("Enter your limit: "))
asknum1()
asknum2()
calculator()
thanks()

But I am getting an error message saying that:

The sum of 2 numbers are: Traceback (most recent call last): line 31, in <module> calculator() line 20, in calculator print(num1 + num2) NameError: name 'num1' is not defined

I am new to python and stuck, need help right now!

When doing the following you create a variable num2 local to the method, it can only accessed in the method's scope, you need to return values from the method in one way and pass them as parameter in another way

def ans_num2():
    num2 = input("Enter your 2st num: ")

Giving :

def welcome():
    print("Welcome user\n")

def asknum(limit, nb):
    res = int(input("Enter your number %s " % nb))
    while res > limit:
        res = int(input("Enter your number %s " % nb))
    return res

def calculator(num1, num2):
    print("The sum of 2 numbers are: ", num1 + num2, "\n")

def thanks():
    print("Thank you and Goodbye :)")

welcome()
limit = int(input("Enter your limit: "))
num1 = asknum(limit, 1)
num2 = asknum(limit, 2)
calculator(num1, num2)
thanks()

num1 and num2 are local variable ie they dont have scope outside the function they are declared in . to fix them declare them outside the function or add global keyword. also you have written asknum1() and not ans_num1() same with ans_num2()

def welcome():
print("Welcome user")
print("")

def ans_num1():
    global num1                                #num1 is declared globally
    num1 = int(input("Enter your 1st num: "))
    while num1 <= limit1:
        print("Good boy")
        break
    else:
        print("Wrong input")
        ans_num1()

def ans_num2():
   global num2                                 #declared globally
   num2= int(input("Enter your 2st num: "))


def calculator():
    print("The sum of 2 numbers are: ")
    print(num1 + num2)
    print("")

def thanks():
    print("Thank you and Goodbye :)")


welcome()
limit1 = int(input("Enter your limit: "))     #already declared globally
ans_num1()                                    #fixed the function name
ans_num2()
calculator()
thanks()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM