Python Forum
Need some beginner Module help...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some beginner Module help...
#1
Just started Intro to Programming Logic and am having a little trouble understanding modules. Here is the instructional video https://www.youtube.com/watch?time_conti...CZqQ6xEzoY, and I have read up on functions, calling, and returning. Just still a little confused on how to turn this

# Declare Real purchase
#
# Display "Please enter the amount of your purchase."
# Input purchase
# Declare Real state_sales_tax
# Declare Real county_sales_tax
# Declare Real total_after_state_sales_tax
# Declare Real total_after_county_sales_tax
# Declare Real full_amount
#
# Set state_sales_tax = 0.04
# Set county_sales_tax = 0.02
# Set total_after_state_sales_tax = state_sales_tax * purchase
# Set total_after_county_sales_tax = county_sales_tax * purchase
# Set full_amount = total_after_state_sales_tax + total_after_county_sales_tax + purchase
# Display "State tax adds", total_after_state_sales_tax, "to your original purchase of", purchase,
# Display "and county tax adds", total_after_county_sales_tax, "which comes to a grand total of", full_amount

purchase = int(input("Please enter the amount of your purchase."))
state_sales_tax = 0.04
county_sales_tax = 0.02
total_after_state_sales_tax = state_sales_tax * purchase
total_after_county_sales_tax = county_sales_tax * purchase
full_amount = total_after_state_sales_tax + total_after_county_sales_tax + purchase

print("State tax adds", total_after_state_sales_tax, "to your original purchase of", purchase,
"and county tax adds", total_after_county_sales_tax, "which comes to a grand total of", full_amount)
into a modular form. Please help!
Reply
#2
move lines 19-24 into a new file called my_module.py. Then at the top of the old file add import my_module
then add my_module prefix to everything referenced in the module such as my_module.total_after_state_sales_tax instead of total_after_state_sales_tax. Usually you do not put input/print into a module that is imported, but in your root file that is importing a module.
Recommended Tutorials:
Reply
#3
But I haven't even learned about importing .py files yet. I am literally just trying to structure it much more neatly via modules. Here's where I am so far
# Module main():
#   Declare price
#   Set purchase = float
#   Display "Enter the price of the purchase: "
#   Input purchase
#   Call calculate_totals(purchase)
# End Module
#
# Function String calculate_totals(purchase)
#   Declare String calculate_totals
#
#   Set calculate_totals = 
#
# Module calculate_state_tax(purchase):
#   Set state_sales_tax = 0.04
#   Return purchase * state_sales_tax


def main():
    purchase = float(input("Enter the price of the purchase: "))
    calculate_totals(purchase)


def calculate_state_tax(purchase):
    state_sales_tax = 0.04
    return purchase * state_sales_tax


def calculate_county_tax(purchase):
    county_sales_tax = 0.02
    return purchase * county_sales_tax


def calculate_totals(purchase):
    print("Original price", purchase)
    state_tax = calculate_state_tax(purchase)
    print("State tax", state_tax)
    county_tax = calculate_county_tax(purchase)
    print("County tax", county_tax)
    print("Total", purchase + state_tax + county_tax)


main()
Also, the psuedocode is what I'm mainly trying to get right.
Reply


Forum Jump:

User Panel Messages

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