python - 为什么我会收到此错误 "name is not defined"?

标签 python

为什么我会收到此错误?图片里有详细信息。我需要获取 _spent 值来打印适当的次数。所以,假设它运行了循环 3 次,我需要它打印 3。我认为这就是 1 的来源。我不喜欢它! enter image description here

pennies = 10
nickels = 10
dimes = 10
quarters = 10

print("\nWelcome to change-making program.")
in_str = input("\nEnter the purchase price (xx.xx) or `q' to quit: ")

while in_str.lower() != 'q':
    dollar_str, cents_str = in_str.split(".")

    if in_str.lower() == 'q':
        quit()

    in_int = int(float(in_str) * 100)

    if in_int < 0:
        print("Error: purchase price must be non-negative.")
        in_str = input("\nEnter the purchase price (xx.xx) or `q' to quit: ")

    if in_int > 0:
        payment = input("\nInput dollars paid: ")
        payment_int = int(float(payment) * 100)
        change = payment_int - in_int

        #determines if there payment input
        if payment_int < in_int:
            print("Error: Insufficient payment.")
            payment = input("\nInput dollars paid: ")
            payment_int = int(float(payment) * 100)


        if change == 0:
            print("No change.")

        #determines how many quarters, dimes, nickels, and pennies are left
        while change >= 25 and quarters > 0:
            change = change - 25
            quarters_spent = 0
            quarters_spent += 1
            quarters = quarters - quarters_spent
        print(quarters_spent)

        while change >= 10 and dimes > 0:
            change = change - 10
            dimes_spent = 0
            dimes_spent += 1
            dimes = dimes - dimes_spent
        print(dimes_spent)

        while change >= 5 and nickels > 0:
            change = change - 5
            nickels_spent = 0
            nickels_spent += 1
            nickels = nickels - nickels_spent
        print(nickels_spent)

        while change >= 1 and pennies > 0:
            change = change - 1
            pennies_spent = 0
            pennies_spent += 1
            pennies = pennies - pennies_spent

        if quarters == 0 and dimes == 0 and nickels == 0 and pennies == 0:
            print("Error: ran out of coins.")
            quit()

        print("\nCollect Payment Below:")
        print(10 - quarters, "Quarters")

        print("\nStock: ", quarters, "Quarters, ", dimes, " Dimes, ", nickels, " Nickels, ", pennies, " Pennies ")

        in_str = input("\nEnter the purchase price (xx.xx) or `q' to quit: ")

        pennies = pennies
        nickels = nickels
        dimes = dimes
        quarters = quarters

最佳答案

此错误意味着您在尝试使用值 nickels_spent 之前没有定义它。

我猜错误出在这一行:print (nickels_spent)

可能发生的情况是,当您尝试运行它时,用于为该变量赋值的 while 语句条件不成立,因此它没有定义,但您仍然尝试使用它。

尝试在 while 循环之前进行调试,看看那里到底发生了什么。

关于python - 为什么我会收到此错误 "name is not defined"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41946561/

相关文章:

python - 如何更改 matplotlib matshow 中矩阵某些元素的颜色?

python - python 中的curl --data-binary 相当于什么?

python - 每次运行 H2OXGBoostEstimator 时出错

python - 谷歌博客 API : AccessTokenRefreshError:invalid_grand message

python - 当测试与正则表达式 : 'NoneType' object has no attribute 'group' 不对应时,我的脚本返回错误

python - 我怎么知道电子邮件是否使用 Django/Python 正确发送?

python - 如何在终端上运行 Python 脚本?

python - 将输出重定向到文件时(使用 os.system 时)奇怪的 python 行为

java - 从 Java 中的 Tensorflow 服务模型获取 Matrix 响应

python - 根据条件就地修改列