Hello DaniWeb,
I am new and I have a problem thats driving me nuts, i'm sure its very simple to an experienced CS but I'm attempting to execute a couple conditional statements.

basically, i am trying to write a program that determines the amount of shipping costs to inform the user of, in accordance to weight of an item. here is tha homework question followed by the code i have managed to mis-write.

tha company charges the following rates:

weight of package rate per pound
2 pounds or less $2.10
over 2 pounds but not more than 6pounds $3.20
over 6 pounds but not more than 10pounds $4.70
over 10pounds $4.80

write a prog that asks the user to enter tha weight of a package and then displays the shipping charges.

here is what i have whipped up so far....

#determine shipping charges according to weight
print 'Welcome to Shipping Company Rate Calculator'
shipweight=input ('Please Enter the Weight of the Item You Wish To Ship: ')
def shipping_charges():
        if shipweight>=2:
            print 'Your total shipping charges: $2.10'
            elif:
                if shipweight>2 or shipweight<6:
        print 'Your total shipping charges: $3.20'
    else:
        if shipweight>6 or shipweight<10:
        print 'Your total shipping charges: $4.70'
    else:
        if shipweight>10:
        print 'Your total shipping charges: $4.80'
    else:
        print 'Your total shipping charges: $4.80'    
shipping_charges()

I kinda butchered it while attempting to get it to run so there might by some random errors.
but it always tells me 'elif' or 'else' are invalid syntax..
haha maybe i should just re-read tha chapter.
as simple/dumb as this may seem im just looking for a little clarification on the if, elif and else structure. Any constructive input is greatly appreciated! Thanx in advance.

It's very simple, the if statement looks like this

if expression:
    statements

optionally followed by zero or more blocks like

elif expression:
    statements

and finally zero ore only one block like

else:
    statements

The "statements" part may contain other if statements, but for a given if statement, the keywords "if", "elif" and "else" must have the same indentation. :)

Reformatted your code and did simple restructuring and some edits
See if that is what you want and post more queries
Steve

#determine shipping charges according to weight
print 'Welcome to Shipping Company Rate Calculator'
shipweight=input ('Please Enter the Weight of the Item You Wish To Ship: ')
def shipping_charges():
        if shipweight>=2:
                print 'Your total shipping charges: $2.10'
        
        elif  shipweight>2 or shipweight<6:
                        print 'Your total shipping charges: $3.20'
        
        elif shipweight>6 or shipweight<10:
                        print 'Your total shipping charges: $4.70'
        else:
                if shipweight>10:
                        print 'Your total shipping charges: $4.80'
                else:
                        print 'Your total shipping charges: $4.80'    
shipping_charges()
commented: good thinking! +4

If you run the above code, it will not work as expected. You want
if shipweight <= 2: as the first if, and the following elif's should use and
elif shipweight >2 and shipweight <6:
Also, what if the weight equals 6? This code doesn't allow for that. BTW, the "standard" way of doing this kind of thing is to use a container like a list. You eliminate a long string of elif, and you can simply modify the list to add/change/delete weight catagories. (No additional elif necessary if another catagory is added.)

def shipping_charges(package_weight):
     charges_list= [ [10, '4.80'],
                     [ 6, '4.70'],
                     [ 2, '3.20'],
                     [ -0.01, '2.10'] ]
     for weight_list in charges_list:
          print "     checking", weight_list[0], "-->", weight_list
          if package_weight > weight_list[0]:
             print "found"
             return weight_list[1]

print 'Welcome to Shipping Company Rate Calculator'
shipweight=raw_input ('Please Enter the Weight of the Item You Wish To Ship: ')
try:
   weight_package = float(shipweight)
   charges = shipping_charges(weight_package)
   print "Your total shipping charges:", charges
except:
   print "You can only enter a number"

A shorter code using module bisect

from bisect import bisect_left

costs =  [0.0, 2.10, 3.20, 4.70, 4.80]
weights = [0, 2, 6, 10]

def shipping_charges(package_weight):
    return costs[bisect_left(weights, package_weight)]

print 'Welcome to Shipping Company Rate Calculator'

while True:
    shipweight=raw_input ('Please Enter the Weight of the Item You Wish To Ship: ')
    if shipweight == "quit":
        break
    try:
        weight_package = float(shipweight)
        charges = shipping_charges(weight_package)
        print "Your total shipping charges: %.2lf" % charges
    except:
        print "You can only enter a number (or 'quit')"
commented: great suggestion +10

If you want to test what the module bisect suggested by our friend Gribouillis does, run a small program like this ...

# testing module bisect

from bisect import bisect_left

costs =  [0.0, 2.10, 3.20, 4.70, 4.80]
weights = [0, 2, 6, 10]

# iterate through a list of possible weights
for package_weight in [1.5, 4.7, 8.1, 11.5]:
    cost = costs[bisect_left(weights, package_weight)]
    print "a package weighing", package_weight, "costs", cost

"""
my output -->
a package weighing 1.5 costs 2.1
a package weighing 4.7 costs 3.2
a package weighing 8.1 costs 4.7
a package weighing 11.5 costs 4.8
"""

Thanks Gribouillis. I didn't know about bisect.

Also remember that the searched list weights has to be sorted for bisect to work!

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.