[英]How to change a variable value from within a function?
def Q1(score): #defines the first question
print ("\n\n1) Question")
print ("\nA) Option.")
print ("\nB) Option")
print ("\nC) Option.")
ans = input("\nIs it A, B or C? ") #asks for the answer
if ans.upper() == "B": #makes it uppercase if they entered lowercase
print ("Correct!")
score += 1 #adds one to the score
return (score) #returns the score
Q1(score) #function is called
print (score) #score is printed
這是我的代碼,當我運行它時沒有發生錯誤,但是當返回“score”變量時,值被重置為0,為什么? (分數首先定義在第一個function上面,放不下)
Q1 function 返回結果,但您沒有將結果保存到變量中。 做這樣的事情:
score = Q1(score)
print(score)
或者,直接打印返回值:
print(Q1(score))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.