Python Forum
So many things wrong, it runs but not like its supposed to
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
So many things wrong, it runs but not like its supposed to
#1
##
# Author: Kasherwood
# Date: September 12, 2020
# This program takes several several functions and completes all of the processes of a vending machine.
##

#These doc strings are for the power_on function
""" This function sets the machine up. All values must be stored in order to be accessible to the other functions.
Args:
    name- the name of the product
    price- price of the product
    quantity- amount of cans in the machine
Returns:
    this function does not return anything."""
name = 0
price = 0
name = ""
quantity = 0
money = 0

def power_on(name, price, quantity):
    if price <= 0:
        print("Error. Price cannot be a negative.")
    if quantity <= 0:
        print("Error, No supply")



def set_product_name(new_name):
    """ This function allows operator to set/change the name of product.

    ARGS:
        new_name- new name of the product

    Returns:
        this function has no returns
        """
    name == new_name
    if new_name == "":
        new_name = "undefined"


    print("The new product name is:", new_name)




def add_stock(cans_added):
    global quantity
    if cans_added >= 0:
        quantity = cans_added + quantity
    if quantity > 40:
        print("ERROR! Too many, try again.")
    else:
        print("The new stock is", quantity, "cans")



def set_price(new_price):
    """ This function takes one parameter which defines the new price of the product.
    ARGS:
        new_price- new price of the product.
    returns:
        no returns for this program.
        """
    global price
    if new_price <= 0:
        new_price = 0.01
    price = new_price
    print("The new price is:", price)




def insert_coins(money):
    global quantity
    """ This function interacts with all the global variables. Based on the amount entered the function will act a certain way.
    ARGS:
        coins: The amount entered by the customer
        returns:
            coins: if the machine does not have enough stock all of the money provided will be returned.

            change: the difference in amount of coins provided and the price
            """
    if quantity < 1:
        print("OUT OF STOCK")
        return money
    if money < price:
        print("Not enough money, returning coins")
        return money
    else:
        change = money - price
        quantity -= 1
        print("One can of", name, "vended" )
        print("Change = ", change)
        return change



def empty_coins():
    """ This Function zeros the balance of coins retained by the machine. no ARGS or RETURNS"""
    global money
    coins_removed = money - money
    print("Coins Removed:", money)







def report():
    global name, quantity, price, money
    print( "REPORT:", "\n", "==========", "\n", "Product Name:", name, "\n", "Quantity", quantity,"\n", "Unit Price:", price, "\n", "Balance", money )
THIS IS THE END OF MY CODE DOES ANYONE SEE HOW MY PARAMETERS AND VARIABLE ARENT RETAINING THEIR VALUES. THANK YOU!!!!! ALL OF THE INDENTS ARE CORRECT IN PYZO AND THE CODE RUNS BUT NOT HOW ITS MEANT TO.
Reply
#2
What is your code supposed to do? It looks like it's meant to mimic the functionality of a vending machine, but you don't call any of the functions you create. As you have presented it, your code does nothing but assign mostly unused variables and create unused functions.

There are some things in your code that look like they probably aren't what you intend, like using the equality comparison operator (==) on line 38 instead of the assignment operator (=), and failing to make name a global variable within that function. Your empty_coins() function doesn't modify the global variable money, but instead creates a local variable coins_removed that you do not use anywhere else.

There are other issues as well, but it's hard to help you troubleshoot your code when it currently doesn't do anything. Can you include more information on your assignment and where things are going wrong?
Reply
#3
(Sep-25-2020, 08:59 PM)GOTO10 Wrote: What is your code supposed to do? It looks like it's meant to mimic the functionality of a vending machine, but you don't call any of the functions you create. As you have presented it, your code does nothing but assign mostly unused variables and create unused functions.

There are some things in your code that look like they probably aren't what you intend, like using the equality comparison operator (==) on line 38 instead of the assignment operator (=), and failing to make name a global variable within that function. Your empty_coins() function doesn't modify the global variable money, but instead creates a local variable coins_removed that you do not use anywhere else.

There are other issues as well, but it's hard to help you troubleshoot your code when it currently doesn't do anything. Can you include more information on your assignment and where things are going wrong?
I am sorry for not providing all of the information, I can not currently post the driver file as it is not mine to share, it was written by my professor and is for his personal project. The issue with the code is that its not retaining values. Everytime the report function is called the name comes out as nothing, despite the parameters in the driver file. I think it's because i haven't really done anything with it so it's not retaining outside of the function. But, I do not know what to do with it. Thank you for your help in advance and your willingness to look at it, even if you can't provide anything due to lack of the driver.
Reply
#4
Even if you don't have the real driver file, you could write some example code that shows what you want the program to do. Write a call that shows the name isn't kept (and if you do that, you're really close to having a test).

insert_coins() seems a bit odd because it is both print()ing information and returning data. Usually a function does one or the other. (without a call example, it's hard to know if the returned data is useful or correct).

set_product_name() doesn't do anything except print a line. Line 38 is a comparison, not an assignment. Although even if you did assign that variable, it doesn't return anything and that variable is not global. So nothing would be retained. Perhaps you meant to modify the global name?
Reply
#5
(Sep-25-2020, 10:33 PM)kasherwood Wrote: Everytime the report function is called the name comes out as nothing, despite the parameters in the driver file. I think it's because i haven't really done anything with it so it's not retaining outside of the function. But, I do not know what to do with it.

bowlofred and I have both mentioned at this point that you are using a comparison operator on line 38 instead of assigning anything to the variable name, and that you didn't make that variable global. Focus on those issues, fix them, and post your new code.

It's understandable that you are not comfortable posting the driver file if it is not yours to share, but you could still tell us something like "The expected output is This, and my program is outputting That."
Reply
#6
Thank you all very much for your help, I have edited my code so much at this point it looks entirely different then this pervious code. A lot of the replies helped and allowed me to fix a few of the issues. So thank you again!
Reply
#7
You need to write test cases so you can run your code without using the "driver file". Testing code by running an application is time consuming and usually does a poor job at covering test cases. A bonus of writing test cases is you can provide working (on should be working) examples when asking for help on the Python forum.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to use list to solve things hamalainenjuho 4 1,942 Nov-14-2021, 12:57 PM
Last Post: jefsummers
  Not sure what I'm supposed to be doing here. ukr_nate 5 7,469 Jun-14-2017, 01:47 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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