Python Forum
If statements and multiplication
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If statements and multiplication
#1
My assignment is to get company name from the user, get the number of fiberoptic cable needed from the user, evaluate whether that number is greater than 100,250, and 500; then based on that feedback, multiply the numbers by .80, .70, and .50 respectively.
Then display the result, with the company name at the end of the program. The problem I'm having is when I execute the program for the numbers greater than250, and 500 that I am multiplying by .7, and .5, I am getting wrong answers. Which makes me wonder if the program is also multiplying the numbers by .8 for the number greater than 100. I'm sorry I am not great at explaining things
print("Hello! Thank you for choos-oh...it seems that we do not have a company name. Can you help us choose one?")


company_name=input("Please input a name for your company: ")
message="Thank you for choosing"
print(message,company_name.title(),'Please continue on to the selection process below')

amount_of_cable=int(input("Amount of cable needed(in feet): "))
if amount_of_cable>100:
	bulk_discount=(round(float(amount_of_cable)*0.80,2))
	print(bulk_discount)
	input("prompt")
	exit()
if (amount_of_cable>250):
	big_bulk_discount=(round(float(amount_of_cable)*0.70,2))
	print(big_bulk_discount)
	input("prompt")
	exit()
if amount_of_cable>500:
	ultimate_bulk_discount=(round(float(amount_of_cable)*0.50,2))
	print(ultimate_bulk_discount)
	input("prompt")
	exit()
	
val_2=0.87
result=(float(amount_of_cable)*(val_2))


print("The toal cost is:",result)

message_1=("Thank you for using this program, designed by ElRoberto")
print(message_1)


input("Press any key to close this window")
I have tried doing utilizing the "and" function with no effect.
The numbers I am getting are for 251*0.70 I am getting 200.8, and my calculator is giving me 175.70
And for 501*0.50 I am getting 400.80, and my calculator is giving me 250.50.
Reply
#2
If you understood the problem well enough to explain it well you probably wouldn't have the problem in the first place. The catch-22 of programming is you don't know enough to know what it is that you don't know.

Think about the order of your comparisons. If a number is greater than 250 it is also greater than 100. If you first check if it is greater than 100, that test is going to succeed.

The exit() function is not meant to compensate for the programmer not knowing how to write an if statement. Read up on the if statement (if, elif, else) and you will see exit() is not needed for your program.

This is repeated multiple times to make up much of of the code in your program:
    bulk_discount=(round(float(amount_of_cable)*0.80,2))
    print(bulk_discount)
    input("prompt")
If you buy a lot of cable or very little the equation does not change. It is always bulk_discount = quantity * discount. The only difference between different incarnations is the discount number (0.8, 0.7, 0.5). You should limit your if statement to only setting the "discount" and then printing the bulk discount after the if statement. Your program should be about a dozen lines long.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Find the maximum multiplication ercv 3 2,136 Nov-30-2020, 11:55 AM
Last Post: DeaD_EyE
  Multiplication Table Homework mcnhscc39 6 4,581 Nov-25-2018, 04:01 PM
Last Post: mcnhscc39
  creating a 3x3 multiplication table aditvaddi 1 3,612 Jun-18-2018, 06:05 AM
Last Post: volcano63
  Multiplication Table funnybone04 4 5,766 Apr-08-2018, 03:03 AM
Last Post: nilamo
  Nested Loop multiplication table SushiRolz 3 10,208 Feb-28-2018, 04:34 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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