Jump to content

Python Project

namarino
Go to solution Solved by martward,

You need to write if express_shipment == 'n' or express-shipment =='no':

There is a difference in python between " and ' (forgot what it was though)

Also I would use: if "y" in express_shipment, it's easier more compact and more robust.

And you don't need an elif if you are not adding an else. Just check whether the input contains a y and use else for if it doesn't.

Hey guys, I'm just learning Python. I learned Java last year so I understand what's going on generally. But I wrote this code and it's not doing what I want and I can't figure out why.

 

Basically what happens is when I input express_shipment, the if statement doesn't work correctly. I enter yes and it still runs the code for no. Does anyone know what I'm doing wrong here? Thanks!

shipment_weight = 0express_shipment = Noneshipment_charge = 0shipment_distance = 0#Prompts user for weight of shipmentshipment_weight = float(input("Weight of shipment(lbs): "))#If weight of shipment is greater than 50, print 'Too heavy' and program endsif shipment_weight > 50:	print("\nToo heavy!")else:	express_shipment = input("Express shipment? ")	if express_shipment == 'n' or 'no':		shipment_charge = .50 * shipment_weight	elif express_shipment == 'y' or 'yes':		shipment_distance = float(input("Distance of shipment(miles): "))		if shipment_distance > 12450:			print("Out of range of Earth's surface!")		else:			if shipment_weight <= 3:				shipment_charge = (shipment_distance / 200) * 3.00			if 3 < shipment_weight <= 10:				shipment_charge = (shipment_distance / 200) * 6.50			if 10 < shipment_weight <= 20:				shipment_charge = (shipment_distance / 200) * 14.00			if shipment_weight > 20:				shipment_charge = (shipment_distance / 200) * 30.00						print("Shipment charge: " + '$' + shipment_charge)		
Link to comment
Share on other sites

Link to post
Share on other sites

You need to write if express_shipment == 'n' or express-shipment =='no':

There is a difference in python between " and ' (forgot what it was though)

Also I would use: if "y" in express_shipment, it's easier more compact and more robust.

And you don't need an elif if you are not adding an else. Just check whether the input contains a y and use else for if it doesn't.

PSU tier list // Motherboard tier list // Community Standards 

My System:

Spoiler

AMD Ryzen 5 3600, Gigabyte RTX 3060TI Gaming OC ProFractal Design Meshify C TG, 2x8GB G.Skill Ripjaws V 3200MHz, MSI B450 Gaming Plus MaxSamsung 850 EVO 512GB, 2TB WD BlueCorsair RM850x, LG 27GL83A-B

Link to comment
Share on other sites

Link to post
Share on other sites

Skip the " and ' part they're the same, sawry.

PSU tier list // Motherboard tier list // Community Standards 

My System:

Spoiler

AMD Ryzen 5 3600, Gigabyte RTX 3060TI Gaming OC ProFractal Design Meshify C TG, 2x8GB G.Skill Ripjaws V 3200MHz, MSI B450 Gaming Plus MaxSamsung 850 EVO 512GB, 2TB WD BlueCorsair RM850x, LG 27GL83A-B

Link to comment
Share on other sites

Link to post
Share on other sites

Fixed most of it. Your math is just wrong on calculating "express shipping" since it doesn't touch the shipping charge whatsoever.

#!/usr/bin/env pythondef main():	#Prompts user for weight of shipment	shipment_weight = float(input("Weight of shipment(lbs): "))	#If weight of shipment is greater than 50, print 'Too heavy' and program ends	if shipment_weight > 50:		print("\nToo heavy!")		return 0	express_shipment = raw_input("Express shipment? ")	if str(express_shipment).lower() in ['n','no']:		shipment_charge = .50 * shipment_weight	elif express_shipment == 'y' or 'yes':		shipment_distance = float(input("Distance of shipment(miles): "))		if shipment_distance > 12450:			print("Out of range of Earth's surface!")			return 0		if shipment_weight <= 3:			shipment_charge = (shipment_distance / 200) * 3.00		if shipment_weight > 3 and shipment_weight <= 10:			shipment_charge = (shipment_distance / 200) * 6.50		if shipment_weight > 10 and shipment_weight <= 20:			shipment_charge = (shipment_distance / 200) * 14.00		if shipment_weight > 20:			shipment_charge = (shipment_distance / 200) * 30.00				print "Shipment charge: $%g" % shipment_chargeif __name__ == '__main__':	main()

The big issue was that you were doing comparisons on strings that don't make sense. You cannot do "if 10 < some_var <= 10" in pretty much any programming language. You have to use compound boolean comparisons.

--Neil Hanlon

Operations Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

You cannot do "if 10 < some_var <= 10" in pretty much any programming language.

Fortunately Python is one of the few where you can do that.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×