No output main() issue?

Hello. I am new to python and working on a lab assignment that I am trying to run in Python. The issue is that I am getting no output even though I have followed the code multiple times.

I tried to run this code, and I am expecting the following output. I am getting no prompts or output when I try to run this code.

Program Output (with input shown in bold)
Enter the monthly sales: 14650.00
Enter the amount of advanced pay, or
enter 0 if no advanced pay was given.
Advanced pay: 1000.00
The pay is $758.00.

Program Output (with input shown in bold)
Enter the monthly sales: 9000.00
Enter the amount of advanced pay, or
enter 0 if no advanced pay was given.
Advanced pay: 0
The pay is $900.00.

Program Output (with input shown in bold)
Enter the monthly sales: 12000.00
Enter the amount of advanced pay, or
enter 0 if no advanced pay was given.
Advanced pay: 2000.00
The pay is $–560.00.
The salesperson must reimburse
the company

The code that I am attempting to run is below. Thank you in advance. I want to understand this, and appreciate the insight of what I am missing!

# This program calculates a salesperson's pay
# at make your own music
def main():
    # get the amount of sales
    sales = get_sales()

    # get the amount of advanced pay
    advanced_pay = get_advanced_pay()

    # Determine the commission rate
    comm_rate = determine_comm_rate(sales)

    # calc pay
    pay = sales * comm_rate - advanced_pay

    # display the amount of pay
    print(f'The pay is ${pay:,.2f}.')

    # determine whether the pay is negative
    if pay < 0:
        print('The salesperson must reimburse')
        print('the company')


# the get_sales function gets a salesperson's
# monthly sales from the user and returns that value
def get_sales():
    # get the amount of monthly sales
    monthly_sales = float(input('Enter the monthly sales: '))

    # Return the amount entered
    return monthly_sales


# The get_advanced pay function gets the amount of
# advanced pay given to the salesperson and returns
# that amount
def get_advanced_pay():
    # get the amount of advanced pay
    print('Enter the amount of advanced pay, or')
    print('enter 0 if no advanced pay was given.')
    advanced = float(input('Advanced pay: '))

    # return amount entered
    return advanced


# The determine_comm_rate function accepts the
# amount of sales as an argument and returns the
# applicable commission rate.
def determine_comm_rate(sales):
    # determine the commission rate
    if sales < 10000.00:
        rate = 0.10
    elif sales >= 10000 and sales <= 14999.99:
        rate = 0.12
    elif sales >= 15000 and sales <= 17999.99:
        rate = 0.14
    elif sales >= 18000 and sales <= 21999.99:
        rate = 0.16
    else:
        rate = 0.18

    # return the comm rate
    return rate

Add the following at the bottom of your code to call the function:

main()

That is, you have defined your functions. Now you have to call it in order for it to execute. For example, if I define the following function, it won’t do anything until I call it.

def do_something(x, y):
    print('The sum of {} and {} is: {}'.format(x, y, x + y)) 
    print('The factor of {} and {} is: {}'.format(x, y, x*y))

This by itself will do nothing. I have just defined a function. What I now have to do is call the function in order for it to be executed:

do_something(20, 30) # This calls the function.

Since in the definition I have defined it with two parameters, I have to enter two parameters when I am calling the function else I will get an error.

1 Like

@paulcondonjr That’s what people told them on Stack Overflow already…

1 Like

I did add main () to the bottom of the code, ran it, with same results of no input or output. Very strange!

# This program calculates a salesperson's pay
# at make your own music
def main():
    # get the amount of sales
    sales = get_sales()

    # get the amount of advanced pay
    advanced_pay = get_advanced_pay()

    # Determine the commission rate
    comm_rate = determine_comm_rate(sales)

    # calc pay
    pay = sales * comm_rate - advanced_pay

    # display the amount of pay
    print(f'The pay is ${pay:,.2f}.')

    # determine whether the pay is negative
    if pay < 0:
        print('The salesperson must reimburse')
        print('the company')


# the get_sales function gets a salesperson's
# monthly sales from the user and returns that value
def get_sales():
    # get the amount of monthly sales
    monthly_sales = float(input('Enter the monthly sales: '))

    # Return the amount entered
    return monthly_sales


# The get_advanced pay function gets the amount of
# advanced pay given to the salesperson and returns
# that amount
def get_advanced_pay():
    # get the amount of advanced pay
    print('Enter the amount of advanced pay, or')
    print('enter 0 if no advanced pay was given.')
    advanced = float(input('Advanced pay: '))

    # return amount entered
    return advanced


# The determine_comm_rate function accepts the
# amount of sales as an argument and returns the
# applicable commission rate.
def determine_comm_rate(sales):
    # determine the commission rate
    if sales < 10000.00:
        rate = 0.10
    elif sales >= 10000 and sales <= 14999.99:
        rate = 0.12
    elif sales >= 15000 and sales <= 17999.99:
        rate = 0.14
    elif sales >= 18000 and sales <= 21999.99:
        rate = 0.16
    else:
        rate = 0.18

    # return the comm rate
    return rate
main()

Unfortunately, the expertise of that group is a much higher level than I would ever reach, and asking a question for help like that makes me bottom of the barrel. That’s fine. I did get a reference to this group.

I am still learning.

I tested your code after adding main() at the bottom and I got results:

test_with_main_added

With the main() addition it should work.

I’m curious does running python get you to the interactive prompt? Maybe your python installation is messed up.

Yes I can confirm this worked! Thank you.

Great!

You’re welcome.

How did it suddenly work without doing anything different?

I did it from the recommendation from stackoverflow and it did not work

Second time it did work.

My guess is that it was a formatting issue or perhaps end user issue.

The most common “I changed the code and it didn’t affect anything, but then I tried again and it worked” explanation is: make sure to save the source code file in your editor before trying to use it again. (And if you want to run the code from scratch, restarting Python is probably a good idea.)

Regarding Stack Overflow: we simply have higher expectations there for trying to diagnose the problem first. A good question should rarely take up anywhere near that much space. If the problem is “nothing happens at all”, then for Stack Overflow you would start by trying to create a smaller program that has the structure that you’re expecting to work, but still “doesn’t do anything” when it should do “something”. Eventually you would get down to something like:

def main():
    print("why don't I see anything?")

and at this point, if you don’t actually know what’s wrong with it, that’s at least what code for the question should look like.

… But of course, for fundamental issues like this, we’ve seen them all before, so we would refer you to a duplicate anyway :wink: In this case, this is where I would have sent you if I’d looked at it more closely:

Ok.

My fundamentals skills for python is this.

I have no programming skills with python being my first language.

I am three weeks into my first class. To teach myself, I took the textbook source into python so I could see the source in action. I am still new at building code.

It didn’t work so I started re-writing the code from the textbook because I am sure it was a typographical error. I compile the code multiple times and multiple ways with the same results which was no input and no output, but without errors compiling.

Python states that the source must be saved in order to compile and run to which I did and the source ran with the same results.

In regards to stackoverflow: see above. It will not be beneficial for me to post there so I won’t bother due to the high expectations that I should know what I am doing, and it is clear I do not which is why I was reaching out.

The issue is that this was a textbook error that left the main () functionality omitted unintentionally and that is why I was reaching out for guidance. Some people have the skills to dive right into code and troubleshoot. I learn by going hands on. If there is a mistake, It gets fixed and then apply that knowledge to the next source code that I compile. I am trying to learn by example.

Thanks for your response. Looking forward to contributing and asking more questions.