Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
i keep getting the error above, my guess is that i'm not passing the info correctly
BASE_PAY=900
total_sale=float(input('What\'s your total sale?: '))
def main():
demographics=get_info()
income=get_income()
budget=get_budget(netpay)
print('Total sales is: $',total_sale)
print('Your comission is: $',comission)
print('The gross pay is: $',Gpay)
print('The deductions are: $',deductions)
print('The netpay is: $',netpay)
print('Housing & Utility: $', HnC)
print('Food & Clothing: $', FnC)
print('Entertainment: $', entertainment)
print('Miscellaneous costs: $', misc)
def get_info():
Fname=input('Enter your first name: ')
Lname=input('Enter your last name: ')
gender=input('Please enter your gender(m/f): ')
if gender=='m' or gender =='M':
print('Mr.',Fname,Lname)
else:
print('Ms.',Fname,Lname)
return Fname, Lname, gender
def get_income():
comission=total_sale*0.06
Gpay=BASE_PAY*comission
deductions=Gpay*0.18
netpay=Gpay-deductions
return comission, Gpay, deductions, netpay
def get_budget(netpay):
HnC=netpay*0.45
FnC=netpay*0.20
entertainment=netpay*0.25
misc=netpay*0.10
return Hnc,FnC, entertainment, misc
main()
You have not defined netpay for your function get_budget, you have defined it inside of another function locally get_income and so where you are trying to call it, it cannot be seen. You should create a global variable called netpay and declare it as None. Then you can edit it from inside your get_income function and call it in your get_budget function without returning this error.
Maybe have a read through this to gain an understanding of variable scope in python. https://www.w3schools.com/python/python_scope.asp
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Hello :) I am new to python. I have been challenged to build a script that simply converts us measurements to metric ones and vise versa. Should be simple but when I run the script I am getting an error
"/home/oily/public_html/script.py
Enter us or international for conversion:us
Traceback (most recent call last):
File "/home/oily/public_html/script.py", line 4, in <module>
begin = input("Enter us or international for conversion:")
File "<string>", line 1, in <module>
NameError: name 'us' is not defined"
Why is python not reconizing the us varible that I have assigned? Is it because of the order? I googled this and I am not finding anything I really understand. Thank you beforehand for any assistance.
SCRIPT------------------
#!/usr/bin/env python
begin = input("Enter us or international for conversion:")
import begin
us = input("Please enter the value in international:")
international = input ("Please enter the value in us:")
def start(begin):
if begin == us:
return us
else:
return international
def conversion():
if us:
return us * 0.0348
elif int:
return int * 3.28084
print(conversion)
Only one function needed, need to change the variable us and international to a float:
begin = input("Enter 'us' or 'international' for conversion:")
def start():
if begin == "us":
us = float(input("Please enter the value in international:"))
print(us * 0.0348)
else:
international = float(input ("Please enter the value in us: "))
print(international * 3.28084)
start()
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
# Sample line of text from file (Ford,F150,55000;)
filename = 'carPrice.txt'
def main():
all_prices= {}
try:
with open(filename) as fh:
for line in fh:
make, model, price = line.strip().split(',')
all_prices[(make,model)]=price.strip()
income = float(input("Enter your monthly income amount:"))
print("Your monthly income amount is",income,)
make = input("Enter Make of the car:")
print("You selected a",make,)
model = input("Enter Model of the car:")
print("You selected a",model,)
price_value=0
for x in price:
if x == (make,model):
price_value=price[x]
print("The price of that car is",price_value,)
payment = (price_value* 0.80)/60
print("The monthly payment is",payment,)
if (payment < 0.11*income):
print("The monthly payment of",payment,"= Acceptable Risk")
return "Acceptable"
else:
print("The monthly payment of",payment,"= Unacceptable Risk")
return "Unacceptable"
# Exception added to enable troubleshooting of errors on lines
except OSError as e:
print(e.errno)
if __name__ == '__main__':
main()
With respect, the code seems to be a bit all over the place, specifically in regards to price. If this is an exact copy of your code I think you may have lost track of what 'price' actually is.
For example here:
for x in price:
if x == (make,model):
price_value=price[x]
However, price here is a string value you pulled from the file e.g. £100. You're then iterating over that £, 1, 0, 0 and checking it against the make and model.
Finally you make the price_value an index of this string e.g.
price[x] # could be price["£"]
This would then cause an exception.
I'd go through your code again and look to make sure you're referencing price, price_value and all_prices where you actually want them
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
tip and tax calculator
bill = price + tax + tip
price = raw_input("What is the price of the meal")
tax = price * .06
tip = price * 0.20
what is wrong with my code
I have tried everything
please answer and get back to me
a few things.
bill = price + tax + tip #You can't add up these values BEFORE calculating them
price = raw_input("What is the price of the meal") #raw_input returns a string, not a float which you will need for processing
tax = price * .06 #So here you need to do float(price) * .06
tip = price * 0.20 #And float(price) here as well.
#Then your " bill = price + tax + tip " step goes here
First of all, you can't use variables that you haven't defined: in your code your are using bill = price + tax + tip but your program doesn't even know what price, tax and tip are yet, so that line should be at the end of the code, after you've asked the price and calculated tax and tip.
Then, you have raw_input, this function returns a string, if you want to convert it to a decimal number that you can multiply and add (float) you can use price = float(raw_input("what is the price of the meal"))
Correct that two things and it should work...
Heres a couple of things wrong with the code:
You're trying to calculate the total before some variables have been defined.
The raw_input function returns a string so you can't do proper mathematical calculations before you coerce it into an integer.
In calculate the tips/tax you should use a float with the whole number 1(1.20) to take the whole value of the bill + 20%.
Below is a code snippet that should work how you want and give you something to think about on how to pass dynamic values into the modifiers within the calculate_bill function for custom tip floats and custom tax floats:
def calculate_bill(bill, bill_modifiers):
for modifier in bill_modifiers:
bill = modifier(bill)
return bill
def calculate_tip(bill, percentage=1.20):
return bill * percentage
def calculate_tax(bill, percentage=1.06):
return bill * percentage
if __name__ == '__main__':
bill = int(input("What is the price of the meal: "))
total_bill = calculate_bill(bill, [calculate_tip, calculate_tax])
print(total_bill)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am having issues with a defined name error message in Python. I know there are quite a few responses to this question, but I cannot seem to find one that fits my situation. My code is as follows:
#Gets the input of the property value from the user and calculates the individual and total revenue
def main():
class_A_input = int(input('Please enter the number of Class A seats sold: '))
class_B_input = int(input('Please enter the number of Class B seats sold: '))
class_C_input = int(input('Please enter the number of Class C seats sold: '))
#Declares the cost for each class of ticket
class_A_cost = 20
class_B_cost = 15
class_C_cost = 10
#Passes the variable for each ticket class
class_A(class_A_input, class_A_cost)
class_B(class_B_input, class_B_cost)
class_C(class_C_input, class_C_cost)
#Calculates the total revenue
total_revenue = (class_A_input * class_A_cost) + ,\
(class_B_input * class_B_cost) + (class_C_input * class_C_cost)
print ('Total tickets revenue is $',format(total_revenue,',d'),sep='')
#Calculates the class A revenue
def class_A(A_input, A_cost):
class_A_revenue = A_input * A_cost
print ('The amount of Class A revenue is $',format(class_A_revenue,',d'),sep='')
#Repeat definitions for Class B and Class C
main()
I am running Python 3.6.0 and I am getting the following name error:
total_revenue = (class_A_input * class_A_cost) + ,\
(class_B_input * class_B_cost) + (class_C_input * class_C_cost)
NameError: name 'class_A_input' is not defined
I don't think I am declaring the variable before I use it. I have tried a variety of different solutions with no success. So what am I doing wrong?
This looks like an indentation issue. total_revenue is global and trying to use local variable from main() in its calculation.
p.s. You should learn about functions in order to help you reduce duplication in your code.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
What I was trying do do was make "cash" like a balance. Therefore I want to be able to change that value. But when I run the program, the value of variable "cash" doesn't seem to change when I subtracted from it. The player is supposed to lose cash when they guess wrongly on the dice roll. More info is in the code itself. It would be much appreciated if answers have a brief explaination, im doing this to learn, its purely constructive.
print ""
import time
cash = 5000
print 'you brought','$',cash,'today'
while cash>0:
from random import randint
die = randint(1,1)
while True:
try:
print
choice1 = int(raw_input('First guess: '))
print
choice2 = int(raw_input('Second guess: '))
print
break
except ValueError:
print 'Please, enter a number.'
print 'rolling die..'
time.sleep(3)
if choice1+choice2==die:
#PROBLEM: The operation below does not change the value of cash, why not?.
cash=cash+1000
print cash
print 'you rolled',die
print 'win! you won $1000, you\'re new balance is:',cash
#PROBLEM: The new val of cash should be printed here ^ but it remains as 5000
else:
cash-1000
print 'you rolled',die
print 'lose! you lost $1000, you\'re new balance is:',cash
if cash<0:
print 'Bankrupt.'
time.sleep(3)
quit()
if cash==1000000:
print 'Millionaire!'
break
cash-1000
Here you perform a subtraction, then throw the result away. What you want instead is:
cash = cash - 1000
Or just:
cash -= 1000
cash - 1000
should be
cash -= 1000
Otherwise you aren't assigning cash - 1000 to anything; just evaluating it
In line 28, where you typed cash-1000, you are not changing the cash variable. It should be cash = cash - 1000 or simply cash -= 1000.
Other problems in your code:
Line 6: Don't import a module multiple times. This slows down the program.
Line 20: You are adding the two choices together, but I'm pretty sure you want to check both of them if they equal die. Do this by typing if choice1 == die or choice2 == die: instead. You expected cash to be printed by that line as you commented in line 26, but it never will because the if statement is flawed.