Python Forum

Full Version: error when running code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Homework help - python newbie. I'm trying to run the following code:

def user_input():
    order_amount = float(input('What is the order amount?'))
    State = (str.upper(input('What is the state?')))  # we're assuming they're entering the abbreviation?
    
    return(order_amount, State)

def taxAmt():
    if (State == 'WI'):
        TaxedAmt= order_amount * Tax
    
    return(TaxedAmt)

def print_results():
    if (State == 'WI'):        
        print('The subtotal is ${sub:,.2f}.'.format(sub=order_amount))
        print('The tax is ${tax:,.2f}.'.format(tax=order_amount * Tax))
        print ('The total is ${tot:,.2f}.'.format(tot= order_amount + TaxedAmt))
    else:
        print ('The total is ${tot:,.2f}.'.format(tot= order_amount))

#Define constant            
Tax = .055
                  
print_results(taxAmt(user_input()))
My output should look like this:
Quote:What is the order amount? 10
What is the state? WI
The subtotal is $10.00.
The tax is $0.55.
The total is $10.55.



What is the order amount? 10
What is the state? MN
The total is $10.00
but i'm getting this:
Output:
What is the order amount?10 What is the state?WI
followed by this error:
Error:
TypeError Traceback (most recent call last) <ipython-input-2-5545011f6e00> in <module>() 22 Tax = .055 23 ---> 24 print_results(taxAmt(user_input())) TypeError: taxAmt() takes 0 positional arguments but 1 was given
I don't know what this means. Honestly, I was following the professor's example. What I initially tried instead of "print_results(taxAmt(user_input())" was simply calling "user_input()" but that didn't work either.

Any insights would be apprecited.
Quote:
Error:
---> 24 print_results(taxAmt(user_input())) TypeError: taxAmt() takes 0 positional arguments but 1 was given
def taxAmt():
you give the function a parameter when you call it but you have none when you define the same function.
When you define the function, you need to specify the parameters it will receive. If you want to pass the sate to the taxAmt() function (which is a good thing to do), this is how you would specify the parameter:

def taxAmt(State):
There is more information in the functions tutorial link in my signature.
Thank you both for your reply. I will specify the parameter and try again. Hope it will work :-)

Thanks again!
Hi again - so I reassessed, revised, made some progress, and reached another road block. Initially I did apply the suggestions above, but then when I added my loop everything went crazy... so I started over and that what I have below. As a reminder, my output should be:
Quote:What is the order amount? 10
What is the state? WI
The subtotal is $10.00.
The tax is $0.55.
The total is $10.55.



What is the order amount? 10
What is the state? MN
The total is $10.00

I revised my code to include a loop as I need to provide multiple examples, not just the WI & MN ones noted. I'm able to run WI, I run MN, and then next time I just pressed "enter" when prompted for an order amount. When I do that I get the following error.

Error:
ValueError Traceback (most recent call last) <ipython-input-26-daab947fce18> in <module>() 20 21 ---> 22 problem1() <ipython-input-26-daab947fce18> in problem1() 17 tax = .055 18 ---> 19 user_input() 20 21 <ipython-input-26-daab947fce18> in user_input() 2 def user_input(): 3 while True: ----> 4 order_amount = float(input('What is the order amount?')) 5 if order_amount == '': 6 break ValueError: could not convert string to float:
Here is my revised code:
def problem1():
    def user_input():
        while True:
            order_amount = float(input('What is the order amount?'))
            if order_amount == '':
                break
            else:
                state = (str.upper(input('What is the state?')))  # we're assuming they're entering the abbreviation?
                if (state == 'WI'):       
                    print('The subtotal is ${sub:,.2f}.'.format(sub=order_amount))
                    print('The tax is ${tax:,.2f}.'.format(tax=order_amount * tax))
                    print ('The total is ${tot:,.2f}.'.format(tot= order_amount*tax + order_amount))
                else:
                    print ('The total is ${tot:,.2f}.'.format(tot= order_amount))
 
#Define constant           
    tax = .055
 
    user_input()
 
   
problem1()
I also tried "if order_amount is None" but that didn't work either. What I was trying to accomplish is to have it break the look if nothing is entered for order amount. What am I doing wrong?
Looks like you're not typing in a number.

Before calling float(), print() whatever it is input() is returning, so you can see what's actually there, which should immediately let you know why it's failing.
>>> float('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 
you can either use try/except or if you havn't learned that - check the user input before trying to convert to float