简体   繁体   English

为什么我会收到“回溯(最近一次呼叫最后一次):”错误?

[英]Why am I getting a “ Traceback (most recent call last):” error?

I'm sorry for asking, but I can't find why I'm getting this errors, especially after the program is running. 我很抱歉,但是我找不到为什么我会收到这个错误,特别是在程序运行之后。

The erros to be exact are: 确切的错误是:

>>> 
Welcome! This program will convert measures for you.
Select operation.
1.Miles to Kilometers
2.Fahrenheit to Celsius
3.Gallons to liters
4.Pounds to kilograms
5.Inches to centimeters
Enter your choice by number: 1
Traceback (most recent call last):
  File "C:\Users\Levhitor\Downloads\Mario_Gomez_Lab2.py", line 112, in <module>
    intro()
  File "C:\Users\Levhitor\Downloads\Mario_Gomez_Lab2.py", line 12, in intro
    main()
  File "C:\Users\Levhitor\Downloads\Mario_Gomez_Lab2.py", line 25, in main
    convertMK()
  File "C:\Users\Levhitor\Downloads\Mario_Gomez_Lab2.py", line 44, in convertMK
    input_M = float(raw_input(("Miles: ")))
TypeError: 'int' object is not callable

I don't understand what's happening here. 我不明白这里发生了什么。 Can anyone help me? 谁能帮我?

raw_input = 0
M = 1.6
# Miles to Kilometers 
# Celsius Celsius = (var1 - 32) * 5/9
# Gallons to liters Gallons = 3.6
# Pounds to kilograms Pounds = 0.45
# Inches to centimete Inches = 2.54


def intro():
    print("Welcome! This program will convert measures for you.")
    main()

def main():
    print("Select operation.")
    print("1.Miles to Kilometers")
    print("2.Fahrenheit to Celsius")
    print("3.Gallons to liters")
    print("4.Pounds to kilograms")
    print("5.Inches to centimeters")

    choice = input("Enter your choice by number: ")

    if choice == '1':
        convertMK()

    elif choice == '2':
        converCF()

    elif choice == '3':
        convertGL()

    elif choice == '4':
        convertPK()

    elif choice == '5':
        convertPK()

    else:
        print("Error")


def convertMK():
    input_M = float(raw_input(("Miles: ")))
    M_conv = (M) * input_M
    print("Kilometers: %f\n" % M_conv)
    restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
    if restart == 'y':
        main()
    elif restart == 'n':
        end()
    else:
        print("I didn't quite understand that answer. Terminating.")
        main()

def converCF():
    input_F = float(raw_input(("Fahrenheit: ")))
    F_conv = (input_F - 32) * 5/9
    print("Celcius: %f\n") % F_conv
    restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
    if restart == 'y':
        main()
    elif restart == 'n':
        end()
    else:
        print("I didn't quite understand that answer. Terminating.")
        main()

def convertGL():
    input_G = float(raw_input(("Gallons: ")))
    G_conv = input_G * 3.6
    print("Centimeters: %f\n" % G_conv)
    restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
    if restart == 'y':
        main()
    elif restart == 'n':
        end()
    else:
        print ("I didn't quite understand that answer. Terminating.")
        main()

def convertPK():
    input_P = float(raw_input(("Pounds: ")))
    P_conv = input_P * 0.45
    print("Centimeters: %f\n" % P_conv)
    restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
    if restart == 'y':
        main()
    elif restart == 'n':
        end()
    else:
        print ("I didn't quite understand that answer. Terminating.")
        main()

def convertIC():
    input_cm = float(raw_input(("Inches: ")))
    inches_conv = input_cm * 2.54
    print("Centimeters: %f\n" % inches_conv)
    restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
    if restart == 'y':
        main()
    elif restart == 'n':
        end()
    else:
        print ("I didn't quite understand that answer. Terminating.")
        main()

def end():
    print("This program will close.")
    exit()

intro()

I do not know what is wrong... can anyone help me? 我不知道出了什么问题......任何人都可以帮助我吗? Thanks you! 谢谢!

At the beginning of your file you set raw_input to 0. Do not do this, at it modifies the built-in raw_input() function. 在文件的开头,将raw_input设置为0.不要这样做,在它处修改内置的raw_input()函数。 Therefore, whenever you call raw_input() , it is essentially calling 0() , which raises the error. 因此,无论何时调用raw_input() ,它实质上都是调用0() ,这会引发错误。 To remove the error, remove the first line of your code: 要删除错误,请删除代码的第一行:

M = 1.6
# Miles to Kilometers 
# Celsius Celsius = (var1 - 32) * 5/9
# Gallons to liters Gallons = 3.6
# Pounds to kilograms Pounds = 0.45
# Inches to centimete Inches = 2.54


def intro():
    print("Welcome! This program will convert measures for you.")
    main()

def main():
    print("Select operation.")
    print("1.Miles to Kilometers")
    print("2.Fahrenheit to Celsius")
    print("3.Gallons to liters")
    print("4.Pounds to kilograms")
    print("5.Inches to centimeters")

    choice = input("Enter your choice by number: ")

    if choice == '1':
        convertMK()

    elif choice == '2':
        converCF()

    elif choice == '3':
        convertGL()

    elif choice == '4':
        convertPK()

    elif choice == '5':
        convertPK()

    else:
        print("Error")


def convertMK():
    input_M = float(raw_input(("Miles: ")))
    M_conv = (M) * input_M
    print("Kilometers: %f\n" % M_conv)
    restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
    if restart == 'y':
        main()
    elif restart == 'n':
        end()
    else:
        print("I didn't quite understand that answer. Terminating.")
        main()

def converCF():
    input_F = float(raw_input(("Fahrenheit: ")))
    F_conv = (input_F - 32) * 5/9
    print("Celcius: %f\n") % F_conv
    restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
    if restart == 'y':
        main()
    elif restart == 'n':
        end()
    else:
        print("I didn't quite understand that answer. Terminating.")
        main()

def convertGL():
    input_G = float(raw_input(("Gallons: ")))
    G_conv = input_G * 3.6
    print("Centimeters: %f\n" % G_conv)
    restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
    if restart == 'y':
        main()
    elif restart == 'n':
        end()
    else:
        print ("I didn't quite understand that answer. Terminating.")
        main()

def convertPK():
    input_P = float(raw_input(("Pounds: ")))
    P_conv = input_P * 0.45
    print("Centimeters: %f\n" % P_conv)
    restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
    if restart == 'y':
        main()
    elif restart == 'n':
        end()
    else:
        print ("I didn't quite understand that answer. Terminating.")
        main()

def convertIC():
    input_cm = float(raw_input(("Inches: ")))
    inches_conv = input_cm * 2.54
    print("Centimeters: %f\n" % inches_conv)
    restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
    if restart == 'y':
        main()
    elif restart == 'n':
        end()
    else:
        print ("I didn't quite understand that answer. Terminating.")
        main()

def end():
    print("This program will close.")
    exit()

intro()

I don't know which version of Python you are using but I tried this in Python 3 and made a few changes and it looks like it works. 我不知道你使用的是哪个版本的Python,但我在Python 3中尝试过这个版本并进行了一些更改,它看起来很有效。 The raw_input function seems to be the issue here. raw_input函数似乎是这里的问题。 I changed all the raw_input functions to "input()" and I also made minor changes to the printing to be compatible with Python 3. AJ Uppal is correct when he says that you shouldn't name a variable and a function with the same name. 我将所有raw_input函数更改为“input()”,并且我还对打印进行了微小的更改以与Python 3兼容。当他说你不应该命名变量和具有相同名称的函数时,AJ Uppal是正确的。 See here for reference: 见这里参考:

TypeError: 'int' object is not callable TypeError:'int'对象不可调用

My code for Python 3 is as follows: 我的Python 3代码如下:

# https://stackoverflow.com/questions/27097039/why-am-i-getting-a-traceback-most-recent-call-last-error

raw_input = 0
M = 1.6
# Miles to Kilometers
# Celsius Celsius = (var1 - 32) * 5/9
# Gallons to liters Gallons = 3.6
# Pounds to kilograms Pounds = 0.45
# Inches to centimete Inches = 2.54


def intro():
    print("Welcome! This program will convert measures for you.")
    main()

def main():
    print("Select operation.")
    print("1.Miles to Kilometers")
    print("2.Fahrenheit to Celsius")
    print("3.Gallons to liters")
    print("4.Pounds to kilograms")
    print("5.Inches to centimeters")

    choice = input("Enter your choice by number: ")

    if choice == '1':
        convertMK()

    elif choice == '2':
        converCF()

    elif choice == '3':
        convertGL()

    elif choice == '4':
        convertPK()

    elif choice == '5':
        convertPK()

    else:
        print("Error")


def convertMK():
    input_M = float(input(("Miles: ")))
    M_conv = (M) * input_M
    print("Kilometers: {M_conv}\n")
    restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
    if restart == 'y':
        main()
    elif restart == 'n':
        end()
    else:
        print("I didn't quite understand that answer. Terminating.")
        main()

def converCF():
    input_F = float(input(("Fahrenheit: ")))
    F_conv = (input_F - 32) * 5/9
    print("Celcius: {F_conv}\n")
    restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
    if restart == 'y':
        main()
    elif restart == 'n':
        end()
    else:
        print("I didn't quite understand that answer. Terminating.")
        main()

def convertGL():
    input_G = float(input(("Gallons: ")))
    G_conv = input_G * 3.6
    print("Centimeters: {G_conv}\n")
    restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
    if restart == 'y':
        main()
    elif restart == 'n':
        end()
    else:
        print ("I didn't quite understand that answer. Terminating.")
        main()

def convertPK():
    input_P = float(input(("Pounds: ")))
    P_conv = input_P * 0.45
    print("Centimeters: {P_conv}\n")
    restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
    if restart == 'y':
        main()
    elif restart == 'n':
        end()
    else:
        print ("I didn't quite understand that answer. Terminating.")
        main()

def convertIC():
    input_cm = float(input(("Inches: ")))
    inches_conv = input_cm * 2.54
    print("Centimeters: {inches_conv}\n")
    restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
    if restart == 'y':
        main()
    elif restart == 'n':
        end()
    else:
        print ("I didn't quite understand that answer. Terminating.")
        main()

def end():
    print("This program will close.")
    exit()

intro()

I noticed a small bug in your code as well. 我注意到你的代码中还有一个小错误。 This function should ideally convert pounds to kilograms but it looks like when it prints, it is printing "Centimeters" instead of kilograms. 理想情况下,此功能应将磅数转换为千克,但看起来在打印时,它打印的是“厘米”而不是千克。

def convertPK():
    input_P = float(input(("Pounds: ")))
    P_conv = input_P * 0.45
    # Printing error in the line below
    print("Centimeters: {P_conv}\n")
    restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
    if restart == 'y':
        main()
    elif restart == 'n':
        end()
    else:
        print ("I didn't quite understand that answer. Terminating.")
        main()

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我为什么要得到回溯(最近一次通话是最近一次): - Why am I getting a Traceback (most recent call last): 我正在获取 Traceback Traceback(最近一次通话最后一次): - i am getting Traceback Traceback (most recent call last): 为什么我在第6行上得到Traceback最近的呼叫和文件字符串 - Why I am getting Traceback most recent call last and file string at line 6 我在最近一次通话中遇到了一些错误 - I am getting some error in most recent call last 解释“回溯(最近一次调用最后一次):”错误 - Interpreting a “Traceback (most recent call last):” error Traceback(最近一次通话最后一次)Python 错误 - Traceback (most recent call last) Python Error 我正在获取错误回溯(最近一次调用最近):对于num_guesses中的我:TypeError:&#39;int&#39;对象不可迭代 - Im getting error Traceback (most recent call last): for i in num_guesses: TypeError: 'int' object is not iterable 我在以下程序中遇到错误,因为 KeyError Traceback(最近一次调用最后一次) - I'm getting an error in the following program as KeyError Traceback (most recent call last) 修复回溯(最近一次通话最近)错误? - Fixing Traceback (most recent call last) error? Python 错误回溯(最后一次调用): - Python Error Traceback (most recent call last):
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM