I am trying to make a calculator using classes but I get error as "variable not defined"
What I am trying is (there are more functions in my codes, but the related code is)
def Start():
x = input("Please input what you want do to a number then the number; a whole number.\n(Example pi 2)\nYou can pow (pow 2 3; 2 to the power of 3),pi,square and cube: ").lower()
x = x.split()
Action = x[0]
number = int(x[1])
print ("Your number: " + str(number))
class Cal:
def pi():
print ("Your number multiplied by Pi: " + str(math.pi * number))
def Again():
y = input("Type 'Yes' to do another one if not type 'No': ").lower()
if "yes" in y:
print ("\n")
Start()
Work()
Again()
elif "no" in y:
pass
def Work():
if Action.startswith("pi"):
Cal.pi()
else:
pass
Start()
Work()
Again()
I am getting "Variable not defined only"
I am using Windows 7 and Python 3.3. What could be the possible issue?
You have to explicitely pass your "variables" to the functions that need them. You can read more about this in any programming tutorial (starting with Python's official tutorial). To "get" the variables from the function that sets them you have to return the variable(s). That's really CS101 BTW
As an exemple:
def foo(arg1, arg2):
print arg1, arg2
def bar():
y = raw_input("y please")
return y
what = "Hello"
whatelse = bar()
foo(what, whatelse)
More on this here: http://docs.python.org/2/tutorial/controlflow.html#defining-functions
Fixed version of your script (nb tested on Python 2.7 with a hack for input but should work as is on 3.x):
import math
def Start():
x = input("Please input what you want do to a number then the number; a whole number.\n(Example pi 2)\nYou can pow (pow 2 3; 2 to the power of 3),pi,square and cube: ").lower()
x = x.split()
Action = x[0]
number = int(x[1])
print ("Your number: " + str(number))
return (Action, number)
class Cal:
def pi(self, number):
print ("Your number multiplied by Pi: " + str(math.pi * number))
def Again():
y = input("Type 'Yes' to do another one if not type 'No': ").lower()
if "yes" in y:
print ("\n")
args = Start()
Work(*args)
Again()
elif "no" in y:
pass
def Work(Action, *args):
if Action.startswith("pi"):
Cal().pi(*args)
else:
pass
def Main():
args = Start()
Work(*args)
Again()
if __name__ == "__main__":
Main()
Inside your Start() function, put
global Action
That way, the variable will enter the global scope, so it will be visible from the other functions.
However, this is not good style. Rather, you should pass parameters to other functions instead of relying on global variables.
Related
I am new to coding and I am learning python. I’m trying to write a simple program to test my skills, but I’m having some difficulties with it; I want to turn it into a function in order to make the program cleaner, but I get this error: http://prntscr.com/im5pt7
Here is what I want to put inside a function:
name = input(str("\nFull Name: "))
position = input(str("Position at the company: "))
print("\nConfirm Staff Data:\n")
name_confirm = "Name: %s"%(name)
position_confirm = "Position: %s"%(position)
print(name_confirm)
print(position_confirm)
confirmAns = input("Is the information right? (Y/N)")
if confirmAns == "y" or confirmAns == "Y":
message = "\nSearching for %s"%(name)
print(message)
hoursWorked = int(input("Insert hours worked: "))
if hoursWorked <= 0:
print("Please insert a valid number")
elif hoursWorked > 0:
print("\nCalculete Paycheck")
hourRate = int(input("Insert the rate of each hour worked: "))
bonus = input("If a bonus was given insert it here: ")
fine = input("If a fine was given insert it here: ")
print('\n')
payment = hoursWorked*hourRate-int(fine)+int(bonus)
paymentMsg = "Your Payment is: $%d"%(payment)
print(paymentMsg)
elif confirmAns == "n" or confirmAns == "N":
ctypes.windll.user32.MessageBoxW(0, "The software will close to avoid slowness.", "Warning", 1)
else:
print("Please answer with Y or N")
I've tried this but it did not work.
Here is all the code (working but with out the function so I need to copy and paste code): https://pastebin.com/PA9mxMkk
What is happening is that the function as other statements needs to hold it's code into a new indentation level
print('a')
def test(var):
print(var)
not this way
print('a')
def test(var):
print(var)
because this way it will give you the error that you are seeing.
All python code should be indented after the ':' character, in python the indentation should be 4 spaces, or people use the tab key, your code has an issue with indentation which I can't be bothered finding;
for example a 'class'
class this_is_a_class():
#indentation
#code goes here
pass
or a 'for loop' or 'while loop';
numbers = [0,1,2,3,4,5,6,7,8,9]
for number in numbers:
#indentation
print(number)
x = 0
while x < 10:
#indentation
x += 1
print('This is going to print 10 times')
or an 'if statement';
true_boolean = True
if true_boolean:
#indentation
print(True)
or a 'function';
def function():
#indentation
print('You have called a function')
What is actually happening, is python is reading through your code 'Token' by token and 'interpreting' what your code does. But considering you don't know what a function is; gloss over this paragraph.
Now for your question about how functions work. A function is used organize code. You can call a function multiple times, which makes your code more organized and easier to work with, this is why as your code got longer, you ran into this problem; Lets for example say i wanted to print 'hello world' 10 times.
I could write this code on 10 seperate lines;
print("hello world")
print("hello world")
#etc... More chance for error
or I could use a function and call it 10 times;
def say_hello_world():
#indentation
print("hello world")
for each_call in range(0,10):
say_hello_world() #This is the function call
You can also pass 'arguments into a function' for example;
def say_hello(person):
#indentation
print('hello', person)
say_hello('Alex')
Now any words that are in quotations in this answer can be google searched with the word 'python' and you can find out much more about how python works.
I hope this gets you started with python. Although all of these concepts can be used in other programming languages.
The first step which is often difficult in learning python in understanding indentation.
for example.
def hello_world(world):
print("hello ", world)
#your function code goes here.
#you need to indent back to be out of function block.
hello_world("there!")
out: hello there
so in your case it should be like this.
def AnsNo():
name = input(str("\nFull Name: "))
position = input(str("Position at the company: "))
print("\nConfirm Staff Data:\n")
name_confirm = "Name: %s"%(name)
position_confirm = "Position: %s"%(position)
print(name_confirm)
print(position_confirm)
confirmAns = input("Is the information right? (Y/N)")
if confirmAns == "y" or confirmAns == "Y":
message = "\nSearching for %s"%(name)
print(message)
hoursWorked = int(input("Insert hours worked: "))
if hoursWorked <= 0:
print("Please insert a valid number")
elif hoursWorked > 0:
print("\nCalculete Paycheck")
hourRate = int(input("Insert the rate of each hour worked: "))
bonus = input("If a bonus was given insert it here: ")
fine = input("If a fine was given insert it here: ")
print('\n')
payment = hoursWorked*hourRate-int(fine)+int(bonus)
paymentMsg = "Your Payment is: $%d"%(payment)
print(paymentMsg)
elif confirmAns == "n" or confirmAns == "N":
print("working")
else:
print("Please answer with Y or N")
return
I am trying to practice my modular programming with this assignment. I found out how to call the variables into my main function, but for my if statement in evenOdd(), I am not sure how to call the value. I get the error 'randNum' is not defined.
from random import randint
def genNumber():
randNum = randint(0,20000)
print("My number is " + str(randNum))
return (randNum)
def inputNum():
userNum = input("Please enter a number: ")
print("Your number is " + str(userNum))
return (userNum)
def evenOdd():
if randNum%2==0:
print("My number is even")
else:
print("My number is odd")
if userNum%2==0:
print("Your number is even")
else:
print("Your number is odd")
def main():
randNum = genNumber()
userNum = inputNum()
evenOdd()
input("Press ENTER to exit")
main()
You can either define randNum in the global scope or just pass it as a variable to your evenOdd() function, like this evenOdd( randNum ).
I'm doing an assignment that takes a number and tells the user if it is a valid SIN number. I can't get my program to run because of this:
UnboundLocalError: local variable 'isdigit' referenced before assignment.
How do I fix this?
from sinmodule import is_valid
def main():
print "SIN Validator"
print "============="
isdigit()
def isdigit():
int_str = str(sinnumber)
length = (len(int_str))
number = raw_input("Please Enter SIN (Q to quit):")
while length != 8:
print "You did not enter 'Q' or a number."
sinnumber = raw_input("Please Enter SIN (Q to quit):")
if length == 8:
if status == True:
print "The number",number," IS a valid SIN."
else:
print "The number",number,"is NOT a valid SIN."
if number == "Q":
print "Good-bye!"
main()
You shouldn't have any issues with UnboundLocalError on isdigit in your code. It is defined in one place, and called as a function inside of a second function. The rules of scoping apply.
However you have not defined the variable sinnumber by the time you use it:
def isdigit():
int_str = str(sinnumber) # <--- here this is unbound
length = (len(int_str))
number = raw_input("Please Enter SIN (Q to quit):")
while length != 8:
print "You did not enter 'Q' or a number."
sinnumber = raw_input("Please Enter SIN (Q to quit):") #<-defined here
...
You need to move the definition (in the while loop) to before you use it. Also, check to see how you're "growing" sinnumber, and where you're updating length
I have created a python calculator, I need to get it to restart, I have added the loop:
#This line defines the end of the program so it restarts.
def sys():
#These lines will define each operation.
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def Multiply(x, y):
return x * y
def Divide(x, y):
return x/y
#This asks the user what operation they would like to use
print("Please select operation ")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
#This tells the user to enter the number of the operation they would like
operation = input("Enter operation(1/2/3/4):")
#This asks the user to input the two numbers they would like the calculator to calculate
num1 = int(input("Please enter first number: "))
num2 = int(input("Please enter second number: "))
#This is the part of the program that will calculate the calculations
if operation == '1':
print(num1, "+", num2, "=", add(num1,num2))
elif operation == '2':
print(num1,"-",num2,"=", subtract(num1,num2))
elif operation == '3':
print(num1,"*",num2,"=", subtract(num1,num2))
elif operation == '4':
print(num1,"/",num2,"=", subtract(num1,num2))
else:
print("Invalid input")
inp = input("Enter clear to play again or exit to exit")
if inp == "clear":
sys()
else:
print("thanks for playing")
sys.exit()
It keeps saying expected an indented block and shows, that it wants the indent in front of:
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def Multiply(x, y):
return x * y
def Divide(x, y):
return x/y
but when I add them in, it keeps saying, that the operation is not defined. I also feel like the loop will not work either.
It looks like you are trying to recursively re-run the code, in which case you probably want e.g.
import sys # so you can use sys.exit()
def add(x, y): # no need for these function definitions to be in the loop
...
...
def main(): # conventional name - sys shadows the module you just imported
print("Please select operation ")
print("1. Add")
...
inp = input("Enter clear to play again or exit to exit")
if inp == "clear":
main()
else:
print("Thanks for playing")
sys.exit() # or just 'return'
if __name__ == "__main__": # if run directly, rather than imported
main()
You can define functions within other functions (although there's no need here), but remember you need another level of indentation:
def outer(n):
def inner(x):
return x ** 2
return 2 * inner(n)
Note that using recursion means you will eventually hit the system recursion depth limit; iteration is probably wiser:
def main():
while True:
...
inp = input("Enter clear to play again or exit to exit")
if inp != "clear":
print("Thanks for playing")
break
You may be interested in this solution to your problem. It makes use of the operator module, which provides named functions for all the standard Python operators. It also uses a list to translate the operation number to a function and a symbol for printing.
from operator import add, sub, mul, div
operations = [
(add, '+'),
(sub, '-'),
(mul, 'x'),
(div, '/'),
]
while True:
print('1. Add')
print('2. Subtract')
print('3. Multiply')
print('4. Divide')
op = int(raw_input('Enter operation (1/2/3/4): '))
if 0 < op <= len(operations):
func, sym = operations[op-1]
num1 = float(raw_input('Please enter first number: '))
num2 = float(raw_input('Please enter second number: '))
print('{} {} {} = {}'.format(num1, sym, num2, func(num1, num2)))
else:
print('Invalid operation')
continue
while True:
inp = raw_input('Enter clear to play again or exit to exit: ')
if inp == 'exit':
print('Thanks for playing')
exit()
elif inp == 'clear':
break
output
1. Add
2. Subtract
3. Multiply
4. Divide
Enter operation (1/2/3/4): 3
Please enter first number: 8
Please enter second number: 9
8.0 x 9.0 = 72.0
Enter clear to play again or exit to exit: exit
Thanks for playing
If you have an empty function, put in the phrase pass as in:
def some_func():
pass
And that error will no longer occur.
I am trying to make a game and I am really stuck. The problem is that I cant figur out how to use object oriented programming correctly. The program should launch gameboard function
everytime when the number doesnt equal to arv. It should return the new board with one "O"
less.
from random import randint
import time
class Game():
def __init__(self):
pass
def beginning(self):
print("How are you, and why are you playing my game?")
bla = str(input())
time.sleep(2)
print("Hello," + bla + ", I am glad to see you!")
time.sleep(2)
print("Anyways, the you have to guess a number from 1-20")
def gameboard(self):
self.__board = ["O","O","O","O","O"]
print(self.__board)
self.__board.pop()
return self.__board
def game(self):
number = randint(1,20)
print(number)
x = 1
arv = input()
self.arv__ = arv
while 5 > x:
if arv == number:
print("Lucky")
break
elif arv != number:
print ("haha, try again")
print("one life gone")
return gameboard()
print(self.board())
x += 1
def Main():
math = Game()
math.beginning()
math.game()
Main()
Using object-oriented programming when you only ever need one instance of the object tends to overcomplicate the program. I suggest having only one main function.
Nevertheless, I fixed your program. Try to find the changes yourself because I am too lazy to explain them, sorry.
from random import randint
import time
class Game():
def __init__(self):
pass
def beginning(self):
print("How are you, and why are you playing my game?")
bla = str(input())
time.sleep(2)
print("Hello," + bla + ", I am glad to see you!")
time.sleep(2)
print("Anyways, the you have to guess a number from 1-20")
self.__board = ["O","O","O","O","O"]
def gameboard(self):
print(self.__board)
self.__board.pop()
return self.__board
def game(self):
number = randint(1,20)
print(number)
x = 1
while 5 > x:
arv = input()
self.arv__ = arv
if arv == number:
print("Lucky")
break
elif arv != number:
print ("haha, try again")
print("one life gone")
self.gameboard()
print(self.__board)
x += 1
def Main():
math = Game()
math.beginning()
math.game()
Main()
Here is a version of your program that avoids OO and is much more simplified:
from random import randint
lives = 5
print("Guess a number from 1 to 20")
number = randint(1, 20)
while (lives > 1 and number != int(input())):
print("incorrect")
print("lives: " + "O" * lives)
lives -= 1
if lives == 0:
print("The number was actually " + str(number))
else:
print("You were right")