Python Forum
Syntax error for code that seems correct
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax error for code that seems correct
#1
I'm pretty new to programming and Python so there may be other issues with my code here. The error is in line 8 of the code.

I keep getting a syntax error on the = and if I delete that I get it on the <. I've crossed referenced with my Python book but it looks good. Any ideas as to what the issue is?


#Display message
print("Shipping Calculator")

#Get data
choice = "y"
while choice.lower() == "y":
    cost = float(input("Cost of items ordered: "))
    if cost  >= 0 and <= 30.00:
        shipping = float(5.95)
        print("Shipping cost      " + str(shipping))
    elif cost <= 49.99:
        shipping = float(7.95)
        print("Shipping cost:     " + str(shipping))
    elif cost <= 74.99:
        shipping = float(9.95)
        print("Shipping cost:     " + str(shipping))
    elif cost > 74.99:
        shipping = float(0)
        print("Shipping cost:     " + str(shipping))
    elif cost < 0:
        print("Error! Please enter a positive number and try again. ")
    total = cost + shipping
    print("Total Cost:        " + str(total))
    #Get user choice
    choice = input("Would you like another calculation? y/n: ")

#Display Bye
print("Bye!")
Reply
#2
There's a value missing to compare to
if cost  >= 0 and cost <= 30.00
better yet:
if cost 0 <= cost <= 30.00
Reply
#3
(Nov-14-2018, 09:47 PM)j.crater Wrote: There's a value missing to compare to
if cost  >= 0 and cost <= 30.00
better yet:
if cost 0 <= cost <= 30.00

The first suggestion worked, but for some reason the second one did not. Either way it fixed my problem as I was overlooking the value. Oddly enough, in the Murach Python book I have it was missing the value as well. I had to adjust some other lines to ensure that the user received an error message for a negative number but it's running as intended now. Thank you.
Reply
#4
(Nov-14-2018, 09:57 PM)mcnhscc39 Wrote: if cost 0 <= cost <= 30.00
Get rid of the first cost:
if 0 <= cost <= 30.00:
Reply
#5
That was clumsy copy-pasting, sorry. And thanks for pointing the typo out nilamo.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Please check whether the code about the for loop question is correct. (SyntaxError) lilliancsk01 10 2,587 Nov-08-2022, 01:25 PM
Last Post: deanhystad
Shocked School project -- need help with it it says syntax error XPGKNIGHT 6 3,312 Aug-11-2022, 08:43 PM
Last Post: deanhystad
  Is that code correct? Sameh 7 2,972 May-22-2021, 04:29 PM
Last Post: Sameh
  I'm getting a syntax error very early on in my code and I can't quite figure it out. liloliveoil 1 2,003 Oct-30-2020, 05:03 AM
Last Post: deanhystad
  Unspecified syntax error hhydration 1 2,006 Oct-25-2020, 10:45 AM
Last Post: ibreeden
  Annuity function for school - syntax error peterp 2 1,972 Oct-12-2020, 10:34 PM
Last Post: jefsummers
  Invalid syntax error, where? tucktuck9 2 3,420 May-03-2020, 09:40 AM
Last Post: pyzyx3qwerty
  Raise an exception for syntax error sbabu 8 3,150 Feb-10-2020, 01:57 AM
Last Post: sbabu
  syntax error: can't assign to operator liam 3 4,042 Jan-25-2020, 03:40 PM
Last Post: jefsummers
  Self taught , (creating a quiz) syntax error on my array DarkAlchemyXEX 9 4,254 Jan-10-2020, 02:30 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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