Python 2.7.16 - Name not defined - python

I have the following code, however when I run it, the variable stored under employee_name is stated as "Name '[name inputted]' is not defined.
I've tried to comment it out, and the issue just arises with another line. Employee_num needs to be a string, as putting it as an integer claims it to not be iterable.
Can anyone help?
import time
ListNames = []
ListIDs = []
ListComissions = []
total_properties = 0
highest = 0
employee_num = "a"
employee_name = "aa"
employee_id= "a"
employee_properties = "a"
employee_comission = "a"
employee_num = str(input("Enter the number of employees: "))
for y in employee_num:
employee_name = input("Enter the name of the employee: ")
ListNames.append(employee_name)
employee_id = str(input("Enter the ID of the employee: "))
ListIDS.append(employee_id)
employee_properties = int(input("Enter the number of properties sold by the employee: "))
ListProperties.append(employee_properties)
total_properties = total_properties + employee_properties
employee_comission = employee_properties * 500
ListComissions.append(employee_comission)
total_comission = total_comission + employee_comission
for i in ListProperties:
if ListProperties[i] > highest:
highest = ListProperties[i]
i = i + 1
star = ListProperties.index(highest)
print (ListNames[star])
print (ListIDs[star])
print (ListProperties[star])
print (ListComissions[star])
starcomission = ListComissions[star] + (ListComissions[star] * 0.15)
x = 1
for x in employee_num:
if x != star:
print (ListNames[x])
print (ListIDs[x])
print (ListProperties[x])
print (ListComissions[x])
x = x + 1

Related

how to print the sum of salary of employees stored using a class in python

I have created a class to store the details of all the employees and display the same. But I have to display the sum of salary of all the employees. I am unable to do that. Can anyone help me......???
class Employee :
empid = 0
name = None
salary = 0.0
def getInput(self) :
print("Enter the empid :: ", end ="")
self.empid = input()
print("Enter the name :: ", end ="")
self.name = input()
print("Enter the salary :: ", end ="")
self.salary = input()
def display(self) :
print("Employee id = " + str(self.empid))
print("Employee name = " + self.name)
print("Employee salary = " + str(self.salary))
def main( args) :
e = [None] * (3)
i = 0
while (i < 3) :
e[i] = Employee()
e[i].getInput()
i += 1
print("**** Data Entered as below ****")
i = 0
while (i < 3) :
e[i].display()
i += 1
if __name__=="__main__":
Employee.main([])
How to print sum of salary of employees after storing them using this.???
First of all, you need to get main() out of your class. Then, you should actually add the code that calculate the sum of the salaries. sumsalary() isn't declared anywhere in your code, the reason that you are not getting an error, it's because the value of i is greater than 3, so that part isn't reachable.
I updated your code so that it calculates the sum without creating a function.
class Employee :
empid = 0
name = None
salary = 0.0
def getInput(self) :
print("Enter the empid :: ", end ="")
self.empid = input()
print("Enter the name :: ", end ="")
self.name = input()
print("Enter the salary :: ", end ="")
self.salary = input()
def display(self) :
print("Employee id = " + str(self.empid))
print("Employee name = " + self.name)
print("Employee salary = " + str(self.salary))
def main(args):
e = [None] * (3)
i = 0
while (i < 3) :
e[i] = Employee()
e[i].getInput()
i += 1
print("**** Data Entered as below ****")
i = 0
while (i < 3) :
e[i].display()
i += 1
i=0
sum_salary = 0
while(i<3):
sum_salary += int(e[i].salary)
i += 1
print("sum salary: " + str(sum_salary) )
if __name__=="__main__":
main([])

How to call a function in another function for calculations?

def warehouseInventoryCreationBios():
bios = []
warehouseName = ["\nWarehouse: WBS"]
bios.append(warehouseName)
warehouse_initial_quantity_aircondSec = 1000
aircondSec = ["Section: AS", "Parts: compressor", "Part ID: ABS01", "Initial Quantity in the warehouse:", warehouse_initial_quantity_aircondSec, '\n']
bios.append(aircondSec)
.
.
return bios
warehouseInventoryCreationBios()
def updateBiosWarehouseInventory():
bios = warehouseInventoryCreationBios()
warehouseUpdateSupplier = []
name = input('Enter Supplier name: ')
name = name.lower()
id_parts = input('The id of the part: ')
id_parts = id_parts.upper()
order_from_supplier = int(input('How many orders from supplier: '))
warehouseUpdateSupplier.append(name)
warehouseUpdateSupplier.append(id_parts)
warehouseUpdateSupplier.append(str(order_from_supplier))
if name == 'tab':
if id_parts == "ABS01" or id_parts == "TBS05" or id_parts == "BBS02":
if id_parts == "ABS01":
compressor_quantity_warehouse = warehouse_initial_quantity_aircondSec + order_from_supplier
return compressor_quantity_warehouse
.
.
return warehouseUpdateSupplier
updateBiosWarehouseInventory()
Input: Enter Supplier name: tab
The id of the part: abs01
How many orders from supplier: 100
Output: NameError: name 'warehouse_initial_quantity_aircondSec' is not defined
How can I add the value warehouse_initial_quantity_aircondSec in the first function with the warehouse_initial_quantity_aircondSec in the second function
Newbie here, sorry ><
You are trying to use a variable in the second function, which is defined in the first as a local variable, you should return that value to use it:
def warehouseInventoryCreationBios():
bios = []
warehouseName = ["\nWarehouse: WBS"]
bios.append(warehouseName)
warehouse_initial_quantity_aircondSec = 1000
aircondSec = ["Section: AS", "Parts: compressor", "Part ID: ABS01", "Initial Quantity in the warehouse:", warehouse_initial_quantity_aircondSec, '\n']
bios.append(aircondSec)
.
.
# return multiple values (as tuple)
return bios, warehouse_initial_quantity_aircondSec
warehouseInventoryCreationBios() # this line is unnecessary, only calculations with no results used after it
def updateBiosWarehouseInventory():
# receive multiple values
bios, warehouse_initial_quantity_aircondSec = warehouseInventoryCreationBios()
warehouseUpdateSupplier = []
name = input('Enter Supplier name: ')
name = name.lower()
id_parts = input('The id of the part: ')
id_parts = id_parts.upper()
order_from_supplier = int(input('How many orders from supplier: '))
warehouseUpdateSupplier.append(name)
warehouseUpdateSupplier.append(id_parts)
warehouseUpdateSupplier.append(str(order_from_supplier))
if name == 'tab':
if id_parts == "ABS01" or id_parts == "TBS05" or id_parts == "BBS02":
if id_parts == "ABS01":
compressor_quantity_warehouse = warehouse_initial_quantity_aircondSec + order_from_supplier
return compressor_quantity_warehouse
.
.
return warehouseUpdateSupplier
updateBiosWarehouseInventory()

Trying to compare two strings in python

I even used print statements to check if y.name and favourite were the same when checking this and they were yet it still wasn't entering the if statement when using
if y.name == favourite
or
if favourite ==y.name
I'm super confused as to why that is since I thought this was just a standard equality check (The beginning of the code is mostly set up, just included it for context in the case that there was a problem there and not the if statement). Thank you in advance!
class Anime(object):
name: str = ""
year_aired = 0
genre1: str = ""
def __init__(self, name, genre1, year_aired):
self.name = name
self.genre1 = genre1
self.year_aired = year_aired
def _make_anime(name, genre1, year_aired):
anime = Anime()
return anime
animelist = input("Please enter a file with a list of anime\n")
animel = open(animelist, "r")
nolines = animel.readlines()
animearr = []
numanime = -1
for i in nolines:
if i.find("*") != -1:
animearr[numanime].genre1 = i
else:
k = Anime("","", 2018)
k.name = i
animearr.append(k)
numanime += 1
favourite = input("Please enter your favourite anime\n")
favgenre = ""
for y in animearr:
if y.name == favourite:
favgenre = y.genre1
print(favgenre)
I think you should add strip.("\n") before you compare two string.
class Anime(object):
name: str = ""
year_aired = 0
genre1: str = ""
def __init__(self, name, genre1, year_aired):
self.name = name
self.genre1 = genre1
self.year_aired = year_aired
def _make_anime(name, genre1, year_aired):
anime = Anime()
return anime
animelist = input("Please enter a file with a list of anime\n")
animel = open(animelist, "r")
nolines = animel.readlines()
animearr = []
numanime = -1
for i in nolines:
if i.find("*") != -1:
animearr[numanime].genre1 = i
else:
k = Anime("","", 2018)
k.name = i
animearr.append(k)
numanime += 1
favourite = input("Please enter your favourite anime\n")
favgenre = ""
for y in animearr:
if y.name == favourite.strip("\n"):
favgenre = y.genre1
print(favgenre)

"local variable referenced before assignment" but assigning the variable is the first thing i do

I'm in a beginner Python class and my instructor wrote pseudocode for us to follow. I've followed it to a T (I feel) and have been getting bugs no matter how I try changing the program. I'd rather have my mistake quickly pointed out than spend more time mucking about. This is written in Python 2.7.8.
#Anthony Richards
#25 February 2018
#This program is a simple simulation of an order-taking software.
#It demonstrates multiple variables, functions, decisions, and loops.
def main():
declareVariables()
while keepGoing == "y":
resetVariables()
while moreFood == "y":
print "Enter 1 for Yum Yum Burger"
print "Enter 2 for Grease Yum Fries"
print "Enter 3 for Soda Yum"
option = input("Please enter a number: ")
if option == 1:
getBurger()
elif option == 2:
getFries()
elif option == 3:
getSoda()
else:
print "You've made an incorrect entry. Please try again."
moreFood = raw_input("Would you like to order anything else? (y/n): ")
calcTotal(totalBurger, totalFries, totalSoda)
printReceipt(total)
keepGoing = raw_input("Would you like to place another order? (y/n): ")
def declareVariables():
keepGoing = "y"
moreFood = "y"
totalBurger = 0
totalFries = 0
totalSoda = 0
subtotal = 0
tax = 0
total = 0
option = 0
burgerCount = 0
fryCount = 0
sodaCount = 0
def resetVariables():
totalBurger = 0
totalFries = 0
totalSoda = 0
total = 0
tax = 0
def getBurger(totalBurger):
burgerCount = input("Please enter the number of burgers: ")
totalBurger = totalBurger + burgerCount * .99
def getFries(totalFries):
fryCount = input("Please enter the number of fry orders: ")
totalFries = totalFries + fryCount * .79
def getSoda(totalSoda):
sodaCount = input("Please enter the number of drinks: ")
totalSoda = totalSoda + sodaCount * 1.09
def calcTotal(totalBurger, totalFries, totalSoda):
subtotal = totalBurger + totalFries + totalSoda
tax = subtotal * .06
total = subtotal + tax
def printReceipt(total):
print "Your total is $"+str(total)
main()
def func():
x = "this is not global"
func()
# print(x) would throw an error
def func2():
global y
y = "this is global"
func2()
print(y) # This indeed will print it
Although you can use this, this is very, very bad practise.
Scopes. You define the variables, but they only exist in declareVariables. Simply move the references over to inside the function you want (main) so they can exist there. Better yet, merge all the functions into one big one, so you don't have to worry about this (or have them all exist in the global scope [defining them before any functions are defined])

My program for getting the average doesn't quite work

def opdracht3()
a = True
result = 0
waslijst = []
while a:
n = input("Enter a number: ")
if n == "stop":
a = False
else:
waslijst += n
for nummer in waslijst:
result += int(nummer)
eind = result / len(waslijst)
print(eind)
opdracht3()
I want to get the average of the list that is being created, but when I add numbers like 11, the len(waslijst) gets set to 2 instead of 1. Is there another way to get the average, or am I using the len function wrong?
You need use .append method to store all elements in a list.
def opdracht3():
a = True
result = 0
waslijst = []
while a:
n = input("Enter a number: ")
if n == "stop":
a = False
else:
waslijst.append(n)
for nummer in waslijst:
result += int(nummer)
eind = result / len(waslijst)
print(eind)
opdracht3()

Categories

Resources