[英]Def function python: raw_input not store
我是python的新手。 自從2013年開始在大學學習以來,我一直在努力學習如何很好地了解python。 對不起,如果有點混亂。
讓我在下面顯示我的問題。 我有一些def函數看起來像:
def thread_1():
a = input('Value UTS (100) = ')
if a > 100:
print line2
d=raw_input('Dont higher than 100. Input y to repeat : ')
d='y'
if d=='y' :
thread_1()
return a
def thread_2():
b = input('Value UAS (100) = ')
if b > 100:
print line2
d=raw_input('Dont higher than 100. Input y to repeat : ')
d='y'
if d=='y' :
thread_2()
return b
def thread_3():
c = input('Value Course (100) = ')
if c > 100:
print line2
d=raw_input('Dont higher than 100. Input y to repeat : ')
d='y'
if d=='y' :
thread_3()
def thread_4():
value_total = a*50/100+b*30/100+c*20/100
這是我的表達定義到程序列表中
if p==1:
thread_1()
thread_2()
thread_3()
thread_4()
最后,我運行此程序:只要輸入數字正確,但最后顯示如下錯誤代碼的程序:
Traceback (most recent call last): File "ganjil-genap.py", line 71, in <module>
thread_4() File "ganjil-genap.py", line 36, in thread_4
value_total = a*50/100+b*30/100+c*20/100 NameError: global name 'a' is not defined
誰能讓我知道我做錯了什么?
提前致謝。
您可能忘記了thread_*
函數的參數。
例如, thread_4
函數聲明需要如下所示:
def thread_4(a, b, c):
value_total = a*50/100+b*30/100+c*20/100
另外,您還必須在函數調用中為函數提供參數,例如:
if p==1:
a=1, b=2, c=3
thread_1(a, b, c)
thread_2(a, b, c)
thread_3(a, b, c)
thread_4(a, b, c)
您僅在這些函數中定義了在線程_1,線程_2和線程_3上使用的變量a,b和c。 “ a”僅在線程_1內定義,在線程_2內定義b,在線程_3內定義c,但它們不是主程序的全局變量。 該聲明
return a
僅返回變量a的值。
您應該將這些商品設置為全球性。 我認為它應該像這樣:
a=0
def thread_1():
global a
a= raW_input....
這將使您的a,b,c變量成為全局變量。
然后應在thread_4()中將a,b和c作為函數的參數傳遞。
def thread_4(a,b,c):
我認為這應該有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.