-2
def ground_ship(weight):
   if weight <= 2:
      per = 1.50
   elif weight > 2 and weight <= 6:
      per = 3.00
   elif weight > 6 and weight <= 10:
      per = 4.00
   else:
      per = 4.75

cost = (weight*per)+20
return print("Price = $"+str(cost))

x=input("Enter the weight")
ground_ship(x)
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
JGDSH
  • 71
  • 1
  • 1
  • 3
  • You have to cast to integer – Danizavtz Aug 01 '20 at 00:22
  • related: [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – wwii Aug 01 '20 at 00:24
  • besides casting, please do not return print(), since it is a None value. – nagyl Aug 01 '20 at 00:24
  • When I run it, I get `SyntaxError: 'return' outside function` at line 12. It looks like the indenting is wrong. Please provide a [mre]. `ground_ship` might not even be related to the problem. Try just `input()` and see if the same error occurs. How are you running the script? – wjandrea Aug 01 '20 at 00:27
  • BTW welcome to SO! Check out the [tour], and [ask] if you want advice. – wjandrea Aug 01 '20 at 00:29
  • @wjandrea ... Normal debug and run option in PyCharm – JGDSH Aug 03 '20 at 07:44
  • @wjandrea - Thanks..Sure will do :) – JGDSH Aug 03 '20 at 07:45

2 Answers2

1

The error is due to your second last line - you need to change the user's input to a float:

def ground_ship(weight):
   if weight <= 2:
      per = 1.50
   elif weight > 2 and weight <= 6:
      per = 3.00
   elif weight > 6 and weight <= 10:
      per = 4.00
   else:
      per = 4.75
      
   cost = (weight*per)+20
    
   return print("Price = $"+str(cost))

x=float(input("Enter the weight"))
ground_ship(x)
Gavin Wong
  • 1,254
  • 1
  • 6
  • 15
0

In the code you provided you have two lines with bad indentation. In order to make your code to work you need to cast the input to integer before execute the if statements.

Here is a working solution for your problem.

def ground_ship(weight):
   if weight <= 2:
      per = 1.50
   elif weight > 2 and weight <= 6:
      per = 3.00
   elif weight > 6 and weight <= 10:
      per = 4.00
   else:
      per = 4.75

   cost = (weight*per)+20
   return print("Price = $"+str(cost))


x=int(input("Enter the weight: "))
ground_ship(x)

Notice that, if you put a string it will break your program, i would recommend to do some error handling with try catch, before make any assumptions about the value your are reading.

Danizavtz
  • 3,166
  • 4
  • 24
  • 25