Jump to content

Basic Python Book

cheshgoalie21
Go to solution Solved by mkylem,

I'm not too versed in Python, but try "elif 2<x<=6". x is greater than 2 when you're entering something greater than six and 2 is always less than 6 so it will default to the $3 category

I am taking a basic programming class and we are starting to use python and using the book "starting out with Python Third Edition". At the end of each chapter we have to do an exercise. This chapter focuses a lot on if-else and if-elif-else statements so I need to use that. The exercise I am doing goes like this:

 

Write a program that asks the user to enter the weight of a package and then displays the shipping charges. The charges are as follows: 2 pounds or less is $1.50 per pound, Over 2 pounds but not more than 6 pounds is $3.00 per pound, Over 6 pounds but not more than 10 pounds is $4.00 per pound, and then Over 10 pounds is $4.75 per pound.

 

x = float(input('What is the weight of the package?'))
if x <= 2:
    int(input(x*1.50))
elif x >2<=6:
    int(input(x*3.00))
elif x >6<=10:
    int(input(x*4.00))
else:
    int(input(x*4.75))

 

The program works until I input a number that is great than 6 in, it just multiplies it as if it was part of the $3 category.

Any help would be greatly appreciated thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not too versed in Python, but try "elif 2<x<=6". x is greater than 2 when you're entering something greater than six and 2 is always less than 6 so it will default to the $3 category

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, mkylem said:

I'm not too versed in Python, but try "elif 2<x<=6". x is greater than 2 when you're entering something greater than six and 2 is always less than 6 so it will default to the $3 category

Yeah, try that. At least in mathe, the < and > signs always go the same direction in an inequality. It should 

EDIT: In the second elif, it should be 6<x<=10 NOT x>6<=10. yours doesn't actually mean anything.

My Rig:

CPU: Intel i7 5820K @4.3 GHz; Mobo: MSI X99A SLI Plus; Cooler: Cryorig H5 Ultimate; RAM: 16GB HyperX Fury Black; GPU: MSI R9 380 4GB; Case: Fractal Design Define R5 Black w/ Window; SSD: Samsung 850 EVO 256GB and 500GB; HDD: WD Blue 1TB; PSU: EVGA 750W P2

Peripherals:

Monitor: LG 29UM57; Headset: HyperX Cloud II; Keyboard: Gamdias Hermes Mechanical; Mouse: Zowie EC2-A

 

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, mkylem said:

I'm not too versed in Python, but try "elif 2<x<=6". x is greater than 2 when you're entering something greater than six and 2 is always less than 6 so it will default to the $3 category

 

2 minutes ago, vectorx5000 said:

Yeah, try that. At least in mathe, the < and > signs always go the same direction in an inequality.

Below is the updated code and yes it worked thank you very much for the help. Its funny how the littlest things can throw everything off but I guess in the world of computing this just shows how precise everything must be. 

 

x = float(input('What is the weight of the package?'))
if x <= 2:
    int(input(x*1.50))
elif 2<x<=6:
    int(input(x*3.00))
elif 6<x<=10:
    int(input(x*4.00))
else:
    int(input(x*4.75))

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, cheshgoalie21 said:

 

Below is the updated code and yes it worked thank you very much for the help. Its funny how the littlest things can throw everything off but I guess in the world of computing this just shows how precise everything must be. 

 

x = float(input('What is the weight of the package?'))
if x <= 2:
    int(input(x*1.50))
elif 2<x<=6:
    int(input(x*3.00))
elif 6<x<=10:
    int(input(x*4.00))
else:
    int(input(x*4.75))

Yes it does have to be precise, but your inequality didn't make any mathematical sense.

My Rig:

CPU: Intel i7 5820K @4.3 GHz; Mobo: MSI X99A SLI Plus; Cooler: Cryorig H5 Ultimate; RAM: 16GB HyperX Fury Black; GPU: MSI R9 380 4GB; Case: Fractal Design Define R5 Black w/ Window; SSD: Samsung 850 EVO 256GB and 500GB; HDD: WD Blue 1TB; PSU: EVGA 750W P2

Peripherals:

Monitor: LG 29UM57; Headset: HyperX Cloud II; Keyboard: Gamdias Hermes Mechanical; Mouse: Zowie EC2-A

 

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, cheshgoalie21 said:

I am taking a basic programming class and we are starting to use python and using the book "starting out with Python Third Edition". At the end of each chapter we have to do an exercise. This chapter focuses a lot on if-else and if-elif-else statements so I need to use that. The exercise I am doing goes like this:

 

Write a program that asks the user to enter the weight of a package and then displays the shipping charges. The charges are as follows: 2 pounds or less is $1.50 per pound, Over 2 pounds but not more than 6 pounds is $3.00 per pound, Over 6 pounds but not more than 10 pounds is $4.00 per pound, and then Over 10 pounds is $4.75 per pound.

 

x = float(input('What is the weight of the package?'))
if x <= 2:
    int(input(x*1.50))
elif x >2<=6:
    int(input(x*3.00))
elif x >6<=10:
    int(input(x*4.00))
else:
    int(input(x*4.75))

 

The program works until I input a number that is great than 6 in, it just multiplies it as if it was part of the $3 category.

Any help would be greatly appreciated thanks!

As a general rule, inequalities should be "x<y<z" or "x>y>z". (adding the or equal to as necessary.)

My Rig:

CPU: Intel i7 5820K @4.3 GHz; Mobo: MSI X99A SLI Plus; Cooler: Cryorig H5 Ultimate; RAM: 16GB HyperX Fury Black; GPU: MSI R9 380 4GB; Case: Fractal Design Define R5 Black w/ Window; SSD: Samsung 850 EVO 256GB and 500GB; HDD: WD Blue 1TB; PSU: EVGA 750W P2

Peripherals:

Monitor: LG 29UM57; Headset: HyperX Cloud II; Keyboard: Gamdias Hermes Mechanical; Mouse: Zowie EC2-A

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, vectorx5000 said:

As a general rule, inequalities should be "x<y<z" or "x>y>z". (adding the or equal to as necessary.)

For some reason I thought of x >6<=10 being like if x is greater than 6 but less than or equal to 10 then it should do this line of code-> int(input(x*4.00))

 

I wasn't thinking straight or really thinking logically, like I said I'm new to this but thank you for the advice!

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

×