2014-11-24 69 views
1

我很抱歉問,但我找不到爲什麼我得到這個錯誤,特別是在程序運行後。爲什麼我會收到「回溯(最近一次通話最後一次):」錯誤?

的誤差修改是準確的:

>>> 
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 

我不明白這裏發生了什麼。誰能幫我?

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() 

我不知道什麼是錯......任何人都可以幫助我嗎? 謝謝!

+0

刪除的raw_input的,它劇照拋出錯誤: 回溯(最近通話最後一個): 文件 「C:/Users/Levhitor/Mario_Gomez_Lab7a.py」,線111,在 介紹() 文件「C :/Users/Levhitor/Mario_Gomez_Lab7a.py「,第11行,介紹 main() 文件」C:/Users/Levhitor/Mario_Gomez_Lab7a.py「,第24行,在主 convertMK() 文件」C:/用戶/ Levhitor/Mario_Gomez_Lab7a.py「,第43行,在convertMK input_M = float(raw_input((」Miles:「))) NameError:全局名稱'raw_input'未定義 – Charles 2014-11-24 02:56:18

+1

你是否偶然使用python 3?在python 3中,'raw_input()'被稱爲'input()'(而input()'被稱爲'eval(input())',但不這樣做) – SingleNegationElimination 2014-11-24 03:48:28

+0

是的,然後改變'raw_input )''輸入()'。是的,不要使用'eval'! 'eval'是邪惡的。 – 2014-11-24 03:56:52

回答

2

在文件的開始處,您將raw_input設置爲0.不要這樣做,因爲它會修改內置的raw_input()函數。因此,每當您撥打raw_input()時,實質上都會調用​​,這會引發錯誤。要刪除錯誤,請刪除代碼的第一行:

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() 
1

我不知道你使用的是哪個版本的Python,但我想這在Python 3,並提出了一些變化,它看起來像它的工作原理。 raw_input函數似乎是這裏的問題。我將所有raw_input函數改爲「input()」,並且我還對打印進行了細微的更改以與Python 3兼容。AJ Uppal說他不應該命名同名的變量和函數。看到這裏供參考:

TypeError: 'int' object is not callable

我的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() 

我在你的代碼注意到了一個小錯誤也是如此。這個功能應該理想地將磅轉換爲千克,但它看起來像打印時,它正在打印「釐米」而不是千克。

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() 

我希望這有助於。

相關問題