2012-12-05 36 views
1

嘿,大家我介紹python編程,我們正在做第一個獨立的代碼。分配如下:UnboundLocalError:分配前引用的局部變量'print'

Prompt the user for his or her name. Then prompt the user for two numbers and then perform a mathematical operation of your choice on them. Make sure the program works with decimal numbers, and that you print a full equation in response, not just the result: Enter a number: 2.3 Enter another number: 3.6 2.3 – 3.6 = -1.3

所以我進入:

def main1(): 
print("This is program 1!") 
name = input("Please enter your name: ") 
print("Pleased to meet you,", name ,"!") #next line def main2(): 
print("This is program 2!") 
import math 
number = input("Enter a number: ") 
number = float(number) 
numberr = input("Enter another number: ") 
numberr = float(numberr) 
print = ("number + numberr") 

我不停的得到這個:

UnboundLocalError: local variable 'print' referenced before assignment 

幫助!

+0

[Python變量範圍錯誤(可能的重複http://stackoverflow.com/questions/370357/python-可變範圍錯誤) –

+0

Stack Overflow上有'UnboundLocalError'的重複項。屏幕的右側應顯示至少10個。 –

回答

4

您嘗試將值分配給print

您寫道:

print = ("number + numberr") 

但你實際上的意思是:

print(number + numberr) 
+3

我會補充一句,如果您從「Traceback」開始讀取/發佈完整回溯,您將更容易找到問題所在(並在遇到異常時從其他人​​處獲得幫助)。回溯告訴你你的問題是哪一行代碼。 – Iguananaut

+0

omg非常感謝你 – user1880690

相關問題