NameError:未定义全局名称“user_cent”

时间:2015-11-14 14:29:21

标签: python python-2.7

我正在编写一个简单的硬币计数器程序,并在输入所有硬币重量后得到“全局名称未定义错误”。在我的计算块刚好在main()函数中而没有为自己定义函数之前,我没有得到这个错误。然而,我想要两个单独的函数,所以我可以在main()中创建一个while循环,以允许用户重复该程序而无需重新启动它。救命?我在这里看过其他问题,它似乎是关于在main中创建的变量是本地的,并且无法从后续函数中访问,但我有点困惑!这是我的代码:

import time
import math

def intro():
    print "-----     " + "Welcome to CoinCounter" + "     -----\n"

def main():
    print "Please enter all weights in Grams\n"
    user_cent = float(raw_input("Enter the total weight of cents: "))
    user_nickel = float(raw_input("Enter the total weight of nickels: "))
    user_dime = float(raw_input("Enter the total weight of dimes: "))
    user_quarter = float(raw_input("Enter the total weight of quarters: "))
    user_halfdollar = float(raw_input("Enter the total weight of half dollars: "))
    calculation()

def calculation():
    num_cent = user_cent / 2.640
    num_nickel = user_nickel / 5.975
    num_dime = user_dime / 2.268
    num_quarter = user_quarter / 5.670
    num_halfdollar = user_halfdollar / 11.340
    wrap_cent = num_cent / 132
    wrap_nickel = num_nickel / 199
    wrap_dime = num_dime / 113
    wrap_quarter = num_quarter / 226
    wrap_halfdollar = num_halfdollar / 453.6
    value_cent = (wrap_cent * 0.5)
    value_nickel = (wrap_nickel * 2.0)
    value_dime = (wrap_dime * 5.0)
    value_quarter = (wrap_quarter * 10.0)
    value_halfdollar = (wrap_halfdollar * 10.0)
    time.sleep(1)
    total_value = value_cent + value_nickel + value_dime + value_quarter + value_halfdollar
    results()

def results():
    print "\n---   RESULTS   --- " 
    print "\nYou need:" \
          "\n\n    %s cent wrappers\n" \
          "    %s nickel wrappers\n" \
          "    %s dime wrappers\n" \
          "    %s quarter wrappers\n" \
          "    %s half dollar wrappers\n" \
          "\nand the value of this is: " \
          % ("%.0f" % math.ceil(wrap_cent), "%.0f" % math.ceil(wrap_nickel), "%.0f" % math.ceil(wrap_dime), "%.0f" % math.ceil(wrap_quarter), "%.0f" % math.ceil(wrap_halfdollar))           
    print "\n" + "    $%s worth of cents\n    $%s worth of nickels" % ("%.2f" % value_cent, "%.2f" % value_nickel)
    print "    $%s worth of dimes\n    $%s worth of quarters" % ("%.2f" % value_dime, "%.2f" % value_quarter)
    print "    $%s worth of half dollars\n" % ("%.2f" % value_halfdollar)
    print "    Your total amount is:\n" + "    ---   " +  "$%s" % ("%.2f" % total_value) + "   ---"

intro()
main()

2 个答案:

答案 0 :(得分:0)

除非您将函数中修改的变量显式声明为全局变量,否则它们默认为local。这意味着user_cent变量是main函数的本地变量,因此calculation函数无法访问。这可以通过在global user_cent, user_nickle, user_dime, user_quarter, user_halfdollar的开头添加main function行来修复,但此处不需要全局变量。您可以将权重作为参数传递给calculation函数/

答案 1 :(得分:0)

在你学习的这个阶段,你应该假设使用全局变量是错误的方法。一旦您获得了更多经验,就可以更容易地看到使用全局可能是否合适(这比您想象的要少得多)。在此之前,将参数传递给函数并返回值。

以下不是很好的代码,但它足以改善一个答案。     进口时间     导入数学

def intro():
    print "-----     " + "Welcome to CoinCounter" + "     -----\n"


def main():
    print "Please enter all weights in Grams\n"
    user_cent = float(raw_input("Enter the total weight of cents: "))
    user_nickel = float(raw_input("Enter the total weight of nickels: "))
    user_dime = float(raw_input("Enter the total weight of dimes: "))
    user_quarter = float(raw_input("Enter the total weight of quarters: "))
    user_halfdollar = float(raw_input("Enter the total weight of half dollars: "))
    calculation([user_cent, user_nickel, user_dime, user_quarter, user_halfdollar])

def calculation(coins):
    cents, nickels, dimes, quarters, halfdollars

    num_cent = cents / 2.640
    num_nickel = nickels / 5.975
    num_dime = dimes / 2.268
    num_quarter = quarters / 5.670
    num_halfdollar = halfdollars / 11.340

    wrap_cent = num_cent / 132
    wrap_nickel = num_nickel / 199
    wrap_dime = num_dime / 113
    wrap_quarter = num_quarter / 226
    wrap_halfdollar = num_halfdollar / 453.6

    value_cent = (wrap_cent * 0.5)
    value_nickel = (wrap_nickel * 2.0)
    value_dime = (wrap_dime * 5.0)
    value_quarter = (wrap_quarter * 10.0)
    value_halfdollar = (wrap_halfdollar * 10.0)

    time.sleep(1)
    total_value = value_cent + value_nickel + value_dime + value_quarter + value_halfdollar
    results([wrap_cent, wrap_nickel, wrap_dime, wrap_quarter, wrap_halfdollar],
            [value_cent, value_nickel, value_dime, value_quarter, value_halfdollar],
            total_value)

def results(wrappers, values, total):
    print "\n---   RESULTS   --- " 
    print "\nYou need:" \
          "\n\n    %.0f cent wrappers\n" \
          "    %.0f nickel wrappers\n" \
          "    %.0f dime wrappers\n" \
          "    %.0f quarter wrappers\n" \
          "    %.0f half dollar wrappers\n" \
          "\nand the value of this is: " \
          % tuple(map(math.ceil, wrappers))

    print "\n" + "    $%.2f worth of cents\n    $%.2f worth of nickels" %  (values[0], values[1])
    print "    $%.2f worth of dimes\n    $%.2f worth of quarters" % (values[2], values[3])
    print "    $%.2f worth of half dollars\n" % (values[4],)
    print "    Your total amount is:\n    ---   $%2.f   ---" % (total,)
intro()
main()