【问题标题】:CLOSED (Thank you)CLOSED (Thank you)
【发布时间】:2022-12-02 07:31:18
【问题描述】:

Another total noob question: I am not sure why my answer is printing out as a decimal. Also, in the lab the dimes are expected to be listed first, not sure how I screwed that up? I appreciate the help!

Define a function called exact_change that takes the total change amount in cents and calculates the change using the fewest coins. The coin types are pennies, nickels, dimes, and quarters. Then write a main program that reads the total change amount as an integer input, calls exact_change(), and outputs the change, one coin type per line. Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies. Output "no change" if the input is 0 or less.

Your program must define and call the following function. The function exact_change() should return num_pennies, num_nickels, num_dimes, and num_quarters. def exact_change(user_total)

def exact_change(user_total):
    return(num_dollars, num_quarters, num_dimes, num_nickles, num_pennies)

if __name__ == '__main__':   
    input_val = float(input())
    num_dollars = input_val // 100 
    rem=input_val % 100
    num_quarters = rem // 25
    rem = rem % 25
    num_dimes = rem // 10
    rem = rem % 10
    num_nickles = rem // 5
    rem = rem % 5
    num_pennies = rem
    
    if input_val <= 0:
        print("no change")
    else:
        num_dollars = input_val // 100
        conv_dollar = str(num_dollars)
        rem = input_val % 100
        if num_dollars == 1:
            print(conv_dollar + ' dollar')
        elif num_dollars > 1:
            print(conv_dollar + ' dollars')
        
        num_quarters = rem // 25
        conv_quarter = str(num_quarters)
        rem = rem % 25
        if num_quarters == 1:
            print(conv_quarter + ' quarter')
        elif num_quarters > 1:
            print(conv_quarter + ' quarters')
            
        num_dimes = rem // 10
        conv_dime = str(num_dimes)
        rem = rem % 10
        if num_dimes == 1:
            print(conv_dime + ' dime')
        elif num_dimes > 1:
            print(conv_dime + ' dimes')
            
        num_nickels = rem // 5
        conv_nickel = str(num_nickels)
        rem = rem % 5
        if num_nickels == 1:
            print(conv_nickel + ' nickel')
        elif num_nickels > 1:
            print(conv_nickel + ' nickels')
            
        num_pennies = rem
        conv_penny = str(num_pennies)
        rem = rem % 1
        if num_pennies == 1:
            print(conv_penny + ' penny')
        elif num_pennies > 1:
            print(conv_penny + ' pennies')
        

1:Compare output 0 / 1 Output differs. See highlights below. Special character legend Input 45 Your output 1.0 quarter 2.0 dimes Expected output 2 dimes 1 quarter 2:Compare output 1 / 1 Input 0 Your output no change 3:Compare output 0 / 2 Output differs. See highlights below. Special character legend Input 156 Your output 1.0 dollar 2.0 quarters 1.0 nickel 1.0 penny Expected output 1 penny 1 nickel 6 quarters 4:Unit test 0 / 3 exact_change(300). Should return 0, 0, 0, 12 NameError: name 'input_val' is not defined 5:Unit test 0 / 3 exact_change(141). Should return 1, 1, 1, 5 NameError: name 'input_val' is not defined

【问题讨论】:

  • The first line of the main function casts the input value tofloatinstead ofint.

标签: python


【解决方案1】:

I didn't run it, but it seems the code shouldn't produce "floats" on output, yet there is some room for improvement:

  1. Your program is not calling the function exact_change, it only defines it at the top of the module, but it's never called.

  2. Use f-string, not string concatenation and you don't have to explicitly convert to string. You can also use it for adding the "plural" ending or not (this will work for all except pennies, since they change the word, not only append "s"). e.g.

     num_quarters = rem // 25
     plural = "s" if num_quarters > 1 else ""
     print(f"{num_quarters} quarter{plural}"
    
  3. This function exact_change does not make too much sense, it has no logic and only prints. All the logic is happening under main entry point of the program (this one-> if __name__="__main__"). The function should do the logic and be called instead. Also it's good to use verb for functions, nouns for objects. So get_exact_change, calculate_exact_change and so on makes more sense (just for the future, not for your current assignment).

  4. The initial calculations are redundant since you do the again in the else block.

  5. input_val = float(input()) - this without any validation / try...except block is problematic. If user hits enter and does NOT input anything you'll end up with ValueError, since empty string cannot be converted to float.

【讨论】:

    【解决方案2】:

    input_val is a float. // floors the value, but doesn't convert it to an int. When you are about to print it you need to convert it to an int first.

    conv_dime = str(int(num_dimes))
    

    【讨论】:

    • Not really true...just run e.g.type(7//3) in REPL and see for yourself, it's an int
    • @JohnDoe I tried it to verify. It is indeed a float after //. Your example is an int, because you gave it two integers 7 and 3. Try a scenario like the one by dglr007 type(7.5//3) --> trinket.io/python/f7ad7f9864
    • you are right, so your answer is the best - upvote
    • Thank you for the help. I modified the program with your recommendation to convert to int but am still running into an error on line 18 and I'm not sure what to change it to. Traceback (most recent call last): File "main.py", line 18, in <module> input_val = float(input()) EOFError: EOF when reading a line
    • @dglr007 I suggest you open another question, close this one, and post your new code there. John Doe suggestions are all correct, but you might have implemented them incorrectly.
    猜你喜欢
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    • 2022-12-02
    • 1970-01-01
    • 2022-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多