问题描述
我想知道如何在函数中使用变量,但在函数之外使用变量。
这是我的代码中的一部分,当答案正确时,它应该将分数加1,然后打印出总分(有多个函数,所以我需要将分数放在)
从函数返回得分,并将其赋值给 b
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ b $地理(分数):
#Question 1
qa1 = input(什么是英格兰的资本?)
如果qa1.lower()==(london):
print(Correct你获得1分)
得分=得分+ 1
其他:
print(不正确)
返回分数
分数=地理)
print(这个测验已经结束,你的分数是,score,。)
I would like to know how I can use a variable in a function but then outside of the function as well.Here is a part of my code which is supposed to add 1 to the score when the answer is correct, then print out the overall score afterwards (there is more than one function so I need to the score to be outside of the function):
score=0 def Geography(score): #Question 1 qa1= input("What is the capital of England? ") if qa1.lower() == ("london"): print ("Correct you gain 1 point") score=score+1 else: print ("Incorrect") Geography(score) print ("This quiz has ended. Your score is " , score, ".")
As you can see, I have tried to use arguments however the code still returns the score as 0 at the end no matter if the person has got the answer right.
Return score from the function and assign it back to score
score=0 def Geography(score): #Question 1 qa1= input("What is the capital of England? ") if qa1.lower() == ("london"): print ("Correct you gain 1 point") score=score+1 else: print ("Incorrect") return score score = Geography(score) print ("This quiz has ended. Your score is " , score, ".")
这篇关于我如何使用变量以便它在函数的内部和外部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!