Python Forum

Full Version: python modules?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey all,

I'm really new to python(I used C/C++ in the past and strongly prefer it). I'm having trouble using "modules". What I got was that they're supposed to be like functions but overly simplified. Python is supposed to be REALLY simple but I feel like I'm going crazy over all my errors being invisible. No bracket, semicolons, proper endings just everything all floating out in the ether.
num = float(input('Enter the cost of your meal: '))


def conver(num):

    while num < 0 or num == 0:
     num = float(input('please enter a positive number: '))
    #rejects numbers 0 or below
    if num > .01 and num < 5.99:
     tip = (num / 100) * 10
    elif num > 6 and num < 12:
      tip = (num/100) * 15
    elif num > 12.01 and num < 17:
       tip = (num/100) * 20
    elif num > 17.01 and num < 25:
       tip = num/100 * 25
    else:
      tip = (num/100) * 30
    return tip
#calculates tip depending on input


def calculate(num, tip):

    tax = (num / 100) * 10
    total = num + tip + tax


print("Food cost: $", round(num))
print("Tip: $",'{0:.1f}'.format(tip))
print("Tax: $",'{0:.1f}'.format(tax))
print("Total: $",'{0:.1f}'.format(total))
#Displays results back to user after calculations.
# The tax and total are set to one digit after the decimal, float values as shown.
#The example showed integer values for 'food' and 'tip' so they were rounded.

conver(num)
calculate(num, tip)
This is basically what I'm working on. It's a mess because I keep trying different thingsnames. The problem I'm having is:

1. tax = (num / 100) * 10
total = num + tip + tax
depending on indentation ONE of those variables disappears/ "Is not used in scope"

2. chunks of the modules work and other don't based on same patterns. Everything ran smoothly before I tried to create 'modules'. Python is REALLY weird to me and our book mostly has pseudocode (which feels pretty useless without keywords or syntax to make things run). Anything I look up looks simple enough and I don't know if something is fundamentally wrong or if I just have a wrong space in the gears.

3. Only one of my print statements will run and local variables aren't acknowledged in the lower mod. I could see numbers being wrong but I can barely tell where things begin and end.
Modules are very simple, and aren't unique in any way to python. In fact, c++ has modules. Other languages might call them different things, though (java, for example, calls them Namespaces). All they are, is a namespaced collection of things (functions, classes, variables, etc), almost always in a different file than whichever file you're currently in.

What you're doing in your snippet, is completely unrelated to modules. Perhaps that's where the confusion is?

Your indentation is wildly erratic. Sometimes you indent a block with 4 spaces, sometimes 3, sometimes 2, sometimes 1. That won't effect python's ability to parse your program, but it sure makes it weird to look at, and could be aiding in the confusion. Most text editors have options you can set for what the 'tab' key does, and using that instead of manually adding each space would definitely help to keep things aligned.

Speaking of indentation, it looks like your print functions are probably supposed to be inside the calculate function, as that's the only place tax or total are used, and you don't return them. ...and it doesn't make sense to print them out before calling the calculate function, because they haven't been calculated yet, so how can they be printed?

conver() returns the tip, but you don't do anything with that return value.

So with all of that mentioned, I'd like to offer a slightly reformatted version of your code:
def conver(num):
    while num < 0 or num == 0:
        num = float(input('please enter a positive number: '))
    #rejects numbers 0 or below
    if num > .01 and num < 5.99:
        tip = (num / 100) * 10
    elif num > 6 and num < 12:
        tip = (num/100) * 15
    elif num > 12.01 and num < 17:
        tip = (num/100) * 20
    elif num > 17.01 and num < 25:
        tip = num/100 * 25
    else:
        tip = (num/100) * 30
    return num, tip
#calculates tip depending on input
 
 
def calculate(num, tip):
    tax = (num / 100) * 10
    total = num + tip + tax
    print("Food cost: $", round(num))
    print("Tip: $",'{0:.1f}'.format(tip))
    print("Tax: $",'{0:.1f}'.format(tax))
    print("Total: $",'{0:.1f}'.format(total))
    #Displays results back to user after calculations.
    # The tax and total are set to one digit after the decimal, float values as shown.
    #The example showed integer values for 'food' and 'tip' so they were rounded.

num = float(input('Enter the cost of your meal: ')) 
num, tip = conver(num)
calculate(num, tip)
I made very small changes in your code so that it now works
num = float(input('Enter the cost of your meal: '))


def conver(num):

    while num < 0 or num == 0:
        num = float(input('please enter a positive number: '))
    #rejects numbers 0 or below
    if num > .01 and num < 5.99:
        tip = (num / 100) * 10
    elif num > 6 and num < 12:
        tip = (num/100) * 15
    elif num > 12.01 and num < 17:
        tip = (num/100) * 20
    elif num > 17.01 and num < 25:
        tip = num/100 * 25
    else:
        tip = (num/100) * 30
    return tip
#calculates tip depending on input


def calculate(num, tip):
    tax = (num / 100) * 10
    total = num + tip + tax
    return total, tax

tip = conver(num)
total, tax = calculate(num, tip)

print("Food cost: $", round(num))
print("Tip: $",'{0:.1f}'.format(tip))
print("Tax: $",'{0:.1f}'.format(tax))
print("Total: $",'{0:.1f}'.format(total))
#Displays results back to user after calculations.
# The tax and total are set to one digit after the decimal, float values as shown.
#The example showed integer values for 'food' and 'tip' so they were rounded.
Tips:
  • Indent python code with 4 spaces everywhere. You can configure your editor to insert 4 space characters when you hit the tab key.
  • The value of variables can be used only after variables have been initialized. After spam = ... you can print spam.
  • You strongly prefer C and C++ but very soon you will strongly prefer python because it will give you easily results that C and C++ can only give after a tremendous programming effort.
  • Read carefully python's error message. They contain a lot of information about your error, such as the line where the error occurred.
(Feb-07-2018, 09:53 PM)Gribouillis Wrote: [ -> ]I made very small changes in your code so that it now works
num = float(input('Enter the cost of your meal: '))


def conver(num):

    while num < 0 or num == 0:
        num = float(input('please enter a positive number: '))
    #rejects numbers 0 or below
    if num > .01 and num < 5.99:
        tip = (num / 100) * 10
    elif num > 6 and num < 12:
        tip = (num/100) * 15
    elif num > 12.01 and num < 17:
        tip = (num/100) * 20
    elif num > 17.01 and num < 25:
        tip = num/100 * 25
    else:
        tip = (num/100) * 30
    return tip
#calculates tip depending on input


def calculate(num, tip):
    tax = (num / 100) * 10
    total = num + tip + tax
    return total, tax

tip = conver(num)
total, tax = calculate(num, tip)

print("Food cost: $", round(num))
print("Tip: $",'{0:.1f}'.format(tip))
print("Tax: $",'{0:.1f}'.format(tax))
print("Total: $",'{0:.1f}'.format(total))
#Displays results back to user after calculations.
# The tax and total are set to one digit after the decimal, float values as shown.
#The example showed integer values for 'food' and 'tip' so they were rounded.
Tips:
  • Indent python code with 4 spaces everywhere. You can configure your editor to insert 4 space characters when you hit the tab key.
  • The value of variables can be used only after variables have been initialized. After spam = ... you can print spam.
  • You strongly prefer C and C++ but very soon you will strongly prefer python because it will give you easily results that C and C++ can only give after a tremendous programming effort.
  • Read carefully python's error message. They contain a lot of information about your error, such as the line where the error occurred.

Ah! So that's the correct format for returning values. I kept thinking the returns should go in a separate/main calling environment. Thank you for the assistance.

(Feb-07-2018, 09:47 PM)nilamo Wrote: [ -> ]Modules are very simple, and aren't unique in any way to python. In fact, c++ has modules. Other languages might call them different things, though (java, for example, calls them Namespaces). All they are, is a namespaced collection of things (functions, classes, variables, etc), almost always in a different file than whichever file you're currently in.

What you're doing in your snippet, is completely unrelated to modules. Perhaps that's where the confusion is?

Your indentation is wildly erratic. Sometimes you indent a block with 4 spaces, sometimes 3, sometimes 2, sometimes 1. That won't effect python's ability to parse your program, but it sure makes it weird to look at, and could be aiding in the confusion. Most text editors have options you can set for what the 'tab' key does, and using that instead of manually adding each space would definitely help to keep things aligned.

Speaking of indentation, it looks like your print functions are probably supposed to be inside the calculate function, as that's the only place tax or total are used, and you don't return them. ...and it doesn't make sense to print them out before calling the calculate function, because they haven't been calculated yet, so how can they be printed?

conver() returns the tip, but you don't do anything with that return value.

So with all of that mentioned, I'd like to offer a slightly reformatted version of your code:
def conver(num):
    while num < 0 or num == 0:
        num = float(input('please enter a positive number: '))
    #rejects numbers 0 or below
    if num > .01 and num < 5.99:
        tip = (num / 100) * 10
    elif num > 6 and num < 12:
        tip = (num/100) * 15
    elif num > 12.01 and num < 17:
        tip = (num/100) * 20
    elif num > 17.01 and num < 25:
        tip = num/100 * 25
    else:
        tip = (num/100) * 30
    return num, tip
#calculates tip depending on input
 
 
def calculate(num, tip):
    tax = (num / 100) * 10
    total = num + tip + tax
    print("Food cost: $", round(num))
    print("Tip: $",'{0:.1f}'.format(tip))
    print("Tax: $",'{0:.1f}'.format(tax))
    print("Total: $",'{0:.1f}'.format(total))
    #Displays results back to user after calculations.
    # The tax and total are set to one digit after the decimal, float values as shown.
    #The example showed integer values for 'food' and 'tip' so they were rounded.

num = float(input('Enter the cost of your meal: ')) 
num, tip = conver(num)
calculate(num, tip)

Thanks for the response. Its been a while since I've done code and I was more focused on getting the text functioning before I cleaned it up. I am used to functions being outside of main and, if I remember, calling them in the correct order the values are returned to the calling environment. Each 'function' is passed back to the main to use when summoned. I realize some python modules are imported but, for the class I'm in, it was shown together on the same page. I don't see the point of trying to dice up the code if it's not being drawn into a separate environment as needed. I guess I'll have to adapt.
Note that currently your if/elif conditions omit certain values - 0.1, 5.99, 6.00, 12, 12.01, 17, 17.01 and they will processed in the else part
Python cares deeply about the amount you indent. To indicate a block of code in Python, you must indent each line of the block by the same amount. A typical amount of indentation for Python is four spaces. This isn't the problem with your code, though, since all of your blocks each have the same amount of indentation for each line.

This is not the primary problem with your code, though. Although you return tip, you didn't assign it to anything outside of the method. It is out of scope when you try to use the result.