def main():
    print('Welcome to the Fast Freight Shipping Company Calculator for Shipping Charges')
    print()
#constants for the shipping prices
    weight_one <= 2
    weight_two > 2
    weight_three > 6
    weight_four > 10

    weight_one_rate = 1.10
    weight_two_rate = 2.20
    weight_three_rate = 3.70
    weight_four_rate = 3.80

#get the weight of the package using the input fucntion
    package_weight = float(input("Please enter the weight of your package: "))
#Calculate shipping costs
    if package_weight == weight_one:
        calc_cost_with_weight_one_rate

    elif package_weight == weight_two:
        calc_cost_with_rate_two_rate

    elif package_weight == weight_three:
        calc_cost_with_rate_three_rate

    else:
        calc_cost_with_weight_four_rate


def calc_cost_with_weight_one_rate():
    cost = package_weight * weight_one_rate

def calc_cost_with_rate_two_rate_():
    cost = package_weight * weight_two_rate

def calc_cost_with_rate_three_rate_():
    cost = package_weight * weight_three_rate

def calc_cost_with_rate_four_rate_():
    cost = package_weight * weight_four_rate

print('Based on a weight of', format(package_weight, '.2f'),\ 'pounds, the shipping charge is $', format(cost, '.2f'),sep=' ')


#calls main
main()

As far as I can tell, my code looks correct, but when I try to run it in Shell, I get the error 'Unexpected Character After Line Continutation Character' and a long space after the last print is highlighted. Hitting backspace deletes the last ), in the print function before main, so I'm not entirely sure how to fix it, and I haven't encountered this syntax error before.

Recommended Answers

All 8 Replies

You can not have any character, including a space, after the line continuation character "\"

print('Based on a weight of', format(package_weight, '.2f'),\ 'pounds, the shipping charge is $', format(cost, '.2f'),sep=' ')

Oh, I see. So, I put it on a new line, and now I'm getting this error:

  File "C:\Users\Mark\Desktop\ITP 100\shipping.py", line 43, in <module>
    print('Based on a weight of', format(package_weight, '.2f'),\
NameError: name 'package_weight' is not defined

This is the first time I've used the if-elif-else structure, so I may not have defined package_weight correctly.

Because in your code, package_weight is a local variable in function main(). It exists only in this function's body.

You got to learn how to pass arguments to and from functions.

Also this does not make sense:

#constants for the shipping prices
    weight_one <= 2
    weight_two > 2
    weight_three > 6
    weight_four > 10

There are different rates for shipping depending on how much the package weights, that's where weight_one and so on comes from.

Concerning passing arguments to other functions, do I simply put it in the parentheses in the other functions? For instance:

def calc_cost_with_weight_one_rate(package_weight):
cost = package_weight * weight_one_rate

Your functions do not return any value.

I'm not sure which part you're referencing. I've only been in my software development class for a few weeks, so this is all still new to me and probably a stupid mistake that I'm not understanding. I'll post my assignment below to see if it can help shed some light on what I'm trying to do:

Write a program that asks the user to enter the weight of a package and then displays the shipping charges. Use functions for calculating the shipping charges and for printing results. Make sure all dollar amounts are formatted as decimal numbers with 2 decimal point precision and that each outputted number includes some text in quotes stating what it is.
Hints: you have multiple options possible so use an if, elif, else structure. This will ensure only one path is chosen.

Thanks for your help everyone!

Study this code, it will teach you how to pass arguments to and from functions:

def main():
    print('Welcome to the Fast Freight Shipping Company Calculator for Shipping Charges')
    print()
    #constants for the shipping prices
    weight_one_rate = 1.10
    weight_two_rate = 2.20
    weight_three_rate = 3.70
    weight_four_rate = 3.80
    #get the weight of the package using the input fucntion
    package_weight = float(input("Please enter the weight of your package: "))
    #Calculate shipping costs
    #weight_one is less than 2 pounds
    if package_weight <= 2:
        cost = calc_cost(package_weight, weight_one_rate)
    #weight_two is more than 2 pounds and less than 6 pounds 
    elif package_weight > 2 and package_weight <= 6:
        cost = calc_cost(package_weight, weight_two_rate)
    #weight_three is more than 6 pounds and less than 10 pounds
    elif package_weight > 6 and package_weight <= 10:
        cost = calc_cost(package_weight, weight_three_rate)
    #weight_four is nore than 10 pounds
    else:
        cost = calc_cost(package_weight, weight_four_rate)

    print('Based on a weight of', format(package_weight, '.2f'),\
         'pounds, the shipping charge is $', format(cost, '.2f'),sep=' ') 

def calc_cost(package_weight, rate):
    cost = package_weight * rate
    return cost

#calls main
main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.