1
\$\begingroup\$

This is a school project that I am turning in late. The prof suggested that i somehow prove myself by improving upon the basic code. This is really tough to do for someone who is just starting to learn to code.

Is there something I can do to:

  1. add onto the code (have it do or ask something different)?
  2. make it even more simple than it probably already is (unlikely)?
  3. though it's counter intuitive, make it more complicated by adding a function or something similar?

I have really looked at this and now that I've actually coded it, I don't know how to improve it.

print("This program computes a cable bill") 
account = input("Enter account number: ") 
cust_type = input("Enter customer type: R (Residental) or B (Business): ") 

def residential(): 
 premium_channels = int(input("Enter number of premium channels used: ")) 
 processing_fee = 4.50 
 basic_service_fee = 20.50 
 premium_channel_fee = 7.50 
 amount = processing_fee + basic_service_fee + (premium_channels * premium_channel_fee) 
 return (amount) 

def business(): 
 basic_connections = int(input("Enter number of basic service connections: ")) 
 premium_channels = int(input("Enter number of premium channels used: ")) 
 processing_fee = 15.0 
 basic_service_fee = 75.0 
 preimum_channel_fee = 50.0 
 if (basic_connections <= 10): 
     amount = processing_fee + basic_service_fee + (premium_channels * preimum_channel_fee) 
 else: 
     amount = processing_fee + basic_service_fee + ((basic_connections - 10) * 5) + (premium_channels * 
preimum_channel_fee) 
 return (amount)

if (cust_type == "R"): 
    bill_amount = residential() 
else: 
    bill_amount = business() 

print("Account number: ", account) 
print("Amount due: ", bill_amount)
\$\endgroup\$
6
  • 3
    \$\begingroup\$ Adding code or functionality is off-topic for this site. Improving existing code is within scope. I've tagged this as homework to indicate that answers should provide hints and suggestions rather than complete code. \$\endgroup\$ Nov 25, 2013 at 4:34
  • 4
    \$\begingroup\$ This question appears to be off-topic because it is about code that is not yours. \$\endgroup\$
    – rolfl
    Nov 25, 2013 at 4:51
  • 1
    \$\begingroup\$ @rolfl: What makes you think that it's not his code? \$\endgroup\$
    – ChrisWue
    Nov 25, 2013 at 5:45
  • \$\begingroup\$ @ChrisWue : This is a school project that I am turning in late. The prof suggested that i somehow prove myself by improving upon the basic code - I take that to mean this is example code he has to improve on.... i.e. he did not write it, but wants to alter it. On second reading, it could also be interpreted that it is his code..... \$\endgroup\$
    – rolfl
    Nov 25, 2013 at 11:33
  • 2
    \$\begingroup\$ @rolfl: Well now that I've actually coded it, I don't know how to improve it. seems a pretty string indicator that he did it himself. \$\endgroup\$
    – ChrisWue
    Nov 25, 2013 at 18:44

3 Answers 3

1
\$\begingroup\$
  • Placing all the input and output in a separate function would improve the structure of program.
  • Computing the bill follows the exact same logic for both types of connection; they are just using a different price list. You could use a data structure to represent the price list, and pass it to the billing function as a parameter.
\$\endgroup\$
0
\$\begingroup\$

If you've learned object-oriented programming, you might see how the residential and business functions could share code by becoming classes (or more likely a class and a subclass)

If you haven't looked at OO, think about refactoring the way 'business' and 'residential' work to avoid having methods which differ only in hard-coded constants.

\$\endgroup\$
0
\$\begingroup\$
  • It is rare for a company to process one invoice at a time; turn it into a function, then write another function which applies it to each row in a .csv file and writes the results to another .csv file.

  • Add some business rules, like 'Residential accounts get 20% off for the first 6 months of their subscription' (this obviously requires a sign-up date and some calendar math).

  • Does the company use different sets of account numbers for business and residential customers? Maybe you could autodetect customer type from the account number instead of prompting for it?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.