Python Forum
error when running code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error when running code
#1
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.
Reply
#2
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.
Recommended Tutorials:
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Thank you both for your reply. I will specify the parameter and try again. Hope it will work :-)

Thanks again!
Reply
#5
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?
Reply
#6
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.
Reply
#7
>>> 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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Running my game file yields a different error on a different computer system Bruizeh 0 1,492 Nov-10-2020, 03:15 AM
Last Post: Bruizeh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020