Reputation: 9
I'm super new to Python and programming in general. I am following an example off of youtube on how to make a simple calculator but I wanted to add my ideas and functionalities. More or less I want to make it very versatile.
This is the part that is not working out for me. I don't know how to make it work using the two methods I created
def main():
while True:
Num1()
num1 = int.num1()
Num2()
num2 = int.num2()
# Executing the specified calculations with the 'Operation' function (row 13)
Operation(num1, num2)
# --> followed by another nested while asking if the user wants to make another calculation
I want the app to read everywhere that if the user types "exit" it will exit, even if it's asking for a number so I created 2 methods to do so because it wasn't working dealing with the loops and user input directly from main()
def Num1():
while True:
num1 = input("What is the first number? ")
if num1.isdigit():
break
elif num1 == 'exit':
print("Thank you using the SimpleCalculator. Bye!")
time.sleep(3)
exit()
else:
print("I don't understand. Please input a valid number")
continue
def Num2():
while True:
num2 = input("What is the second number? ")
if num2.isdigit():
break
elif num2 == "exit":
print("Thank you using the SimpleCalculator. Bye!")
time.sleep(3)
exit()
else:
print("I don't understand. Please input a valid number")
continue
The problem is that I get this error and I get it but I just don't know how to go about it.
Traceback (most recent call last):
File "C:/Users/Albert/PycharmProjects/HelloWorld - First Py'
Apps/SimpleCalculator.py", line 131, in <module>
main()
File "C:/Users/Albert/PycharmProjects/HelloWorld - First Py'
Apps/SimpleCalculator.py", line 109, in main
Operation(num1, num2)
NameError: name 'num1' is not defined
Upvotes: 0
Views: 146
Reputation: 90
I see few general errors in your code:
So, using this, it can look like this way:
def main():
while True:
# just call NumEntering - one general function for entering numbers,
# and give to it attribute - message for user.
num1 = int(NumEntering("What is the first number? "))
num2 = int(NumEntering("What is the second number? "))
# Executing the specified calculations with the 'Operation' function (row 13)
Operation(num1, num2)
And the function:
def NumEntering(message): # message - this what function get from outer function.
while True:
num_str = input(message) # show given message
if num_str.isdigit():
return int(num_str) # convert to int and return to outer function
elif num_str == 'exit':
print("Thank you using the SimpleCalculator. Bye!")
time.sleep(3)
exit()
else:
print("I don't understand. Please input a valid number")
continue
Upvotes: 1
Reputation: 86
It may just be that you have posted your code with omissions but there are several points to address.
The functions Num1 and Num2 do not return anything. Although the value num1 is assigned within the function, it is not accessible outside that function's scope.
int does not have a method "num1" so calling int.num1() should be replaced with int(num1())
Creating 2 nearly identical functions creates a lot more work for yourself and makes the code hard to change, you could create a more general function.
def getnum():
while True:
mynum = input("What is the first number? ")
if mynum.isdigit():
break
elif mynum == 'exit':
print("Thank you using the SimpleCalculator. Bye!")
time.sleep(3)
exit()
else:
print("I don't understand. Please input a valid number")
return mynum
def main():
while True:
num1 = int(getnum())
num2 = int(getnum())
Upvotes: 0
Reputation: 2061
Disclaimer: I'd ask for some clarification, but I don't have the SO privileges yet.
Can you make sure your indentation is correct when you're running the program? For instance, your while loops shouldn't be on the same indentation level as your function definitions.
Also, are you functions all in this file SimpleCalculator.py
? If so, you will need to have an additional line to call your methods, since right now they're only being declared.
if __name__ == '__main__':
# And the following line will call your main function which you defined previously
main()
Better yet, just remove your main()
function and replace it with the more Pythonic syntax:
if __name__ == '__main__':
while True:
Num1()
Num2()
Also, since you're calling Num1()
and Num2()
and they're handling the inputs, you don't need to set num1 = int.num1()
or num = int.num2()
(which both give me errors) or the like. If you want to have the values to be returned to the main method for handling, you'll need to do return num1
when you check if num1.isdigit():
, and in your main method set firstNumberInput = Num1()
Hope this helps!
Upvotes: 0
Reputation: 280
The problem was that Num1() and Num2() asked the questions, but didn't do anything with them. Also, the num1 and num2 that were defined were variables local to the function, so you couldn't access them from the main() function. You should have added "return numX" instead of break and assigned Num1() to num1, which allows you to capture the user's input in a variable accessible by the main() function.
This is what your code should have looked like:
import time
def Num1():
while True:
num1=input("What is the first number? ")
if num1.isdigit():
return int(num1)
elif num1 == 'exit':
print("Thank you using the SimpleCalculator. Bye!")
time.sleep(3)
exit()
else:
print("I don't understand. Please input a valid number")
def Num2():
while True:
num2=input("What is the first number? ")
if num2.isdigit():
return int(num2)
elif num2 == 'exit':
print("Thank you using the SimpleCalculator. Bye!")
time.sleep(3)
exit()
else:
print("I don't understand. Please input a valid number")
def Operation(num_1, num_2):
# code for Operation function
pass
def main():
while True:
num1=Num1()
num2=Num2()
Operation(num1, num2)
if __name__=="__main__": main()
Upvotes: 0
Reputation: 3751
There's plenty wrong with your code.
DRY Do not Repeat Yourself. There's no need to have two functions when they do the same thing. Delete Num2 in its entirety. It's useless.
Num1 isn't returning a value. We want the function to ask the user for a number (which it does), exit if the user inputs "exit" (also done) and return the number entered by the user (not done).
Function names start with a lowercase letter and are descriptive. Num1 does neither, so let's change it to ask_number.
All the problems identified above can be fixed by replacing Num1 and Num2 with the following ask_number function.
def ask_number():
while True:
num = input("What is the first number? ")
if num.isdigit():
return int(num) #This line fixes problem 2
elif num == 'exit':
print("Thank you using the SimpleCalculator. Bye!")
time.sleep(3)
exit()
else:
print("I don't understand. Please input a valid number")
Fix it by replacing your old code
Num1()
num1 = int.num1()
Num2()
num2 = int.num2()
with
num1 = ask_number()
num2 = ask_number()
We're calling the ask_number function twice and assigning its return value to num1 and num2.
Upvotes: 0