Forum Thread: Need Help in Python Code.

Hello once again null-byte,

As programming is an essential thing in hacking, I myself have taken the task of teaching myself how to code but there are always some contingencies But that is what null-byte is for, To get solutions of problems.

What I've been trying to do is make a python calculator and want to make it restart itself at the end with a (y/n) input. But I'm getting this error at the end when I type "y" meaning yes I want to restart the program and "n" to no I want to quit.

here is the error
Traceback (most recent call last):
File "./calculator.py", line 43, in <module>
again()
File "./calculator.py", line 34, in again
option = input("Again?(y/n):")
File "<string>", line 1, in <module>
NameError: name 'n' is not defined

my calculator code

#! /usr/bin/python

def main(): #defining the main() function which has all the calculations.
def addition(x , y):
return(x + y)
def subtraction(x , y):
return(x - y)
def multiplication(x , y):
return(x * y)
def division(x , y):
return(x / y)
print"Welcome To My Python Calculator"

print"1. Addition"
print"2. Subtraction"
print"3. Multiply"
print"4. Divide"
choice = input("Please Choose One Of The Options 1/2/3/4:")

num1 = int(input("Enter First Number:"))
num2 = int(input("Enter Second Number:"))

if choice == 1:
print ("num1 + num2 =" ,addition(num1 , num2))
elif choice == 2:
print ("num1 - num2 =" ,subtraction(num1 , num2))
elif choice == 3:
print ("num1 * num2 =" ,multiplication(num1 , num2))
elif choice == 4:
print ("num1 / num2 =" ,division(num1 , num2))
elif choice >= 5:
print "Invalid Input!!!"

def again(): #defining the parameters of again()
option = input("Again?(y/n):")
if input == "y":
main()
elif input == "n":
print"Thank you for using me!, Have a nice day and Bye Bye~"
quit()

main() #calling main() function.
again()#calling again() function for restarting program.

this page does not support Indentation, I suppose, but I have put the indentations correctly Hope to get a suitable answer asap.

The_Unknown.

3 Responses

Why not just set the question as a variable, and just check if the variable equals y or n? I don't know too much about Python, but I think it might work.

Here is a working code of what you're trying to do:
#!/usr/bin/python

def addition(x, y):
return x + y
def subtraction(x, y):
return x - y
def multiplication(x, y):
return x * y
def division(x, y):
return x / y

print("Welcome to my Python Calculator")

while True:
print("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division")
choice = int(raw_input("Please choose one of the options 1/2/3/4: "))
if choice in 1, 2, 3, 4:
x = int(raw_input("Enter first number: "))
y = int(raw_input("Enter second number: "))
result = choices = {
1: addition(x, y),
2: subtraction(x, y),
3: multiplication(x, y),
4: division(x, y)
}choice
print(result)
option = raw_input("Again? y/N ")
if option.lower().startswith("y"): pass
else:
print("Thank you for using me! Have a nice day and bye bye.")
break
else:print("Invalid input! try again")

Like he said, if option is a variable, then you might just wanna check the y or n like it is a variable, if that made any sense.

Share Your Thoughts

  • Hot
  • Active