Function and Parameters












0















I am making a game where the user should write answer (the question() function). In the question function I am using variables of 1 and 0 to get the information if the user's answer is wrong or correct. I am also using variables of 1 and 0 to see if the user has answered the questions.



def main():
menu()
anv_val = ber_val()
val(anv_val)

def menu():
print("1. game")
print("2. stat")
print("3. end")

def ber_val():
val = input("Your choice: ")
while val not in ["1", "2", "3"]:
print("Print 1, 2 or 3.")
val = input("Your choice: ")
return val

def val(anv_val):
if (anv_val == "1"):
res = game()
return res
elif (anv_val == "2"):
res = game()
return stat(res)
else:
return end()

def question(quest, solu):
print(quest)
answer = input("Your answer: ")
a_s = 1
a_f = 0
while (answer != solu):
a_f = 1
print("Try again")
answer = input("Your answer: ")
print("Correct!")
a_f_s = [a_f, a_s]
return a_f_s

def game():
a_1 = question("Your name?", "Ricky")
a_2 = question("Your name?", "Rong")
a_3 = question("Your name?", "Bolly")
sum_f = a_1[0] + a_2[0] + a_3[0]
sum_s = a_1[1] + a_2[1] + a_3[1]
sum_all = [sum_f, sum_s]
return sum_all, main()

def stat(res):
print("Questions you answered: " + str(res[1]))
print("Wrong times: " + str(res[0]))
return main()

main()


Now to my problem. In the function game(), I want to return the variable called a_f_s, AND USE the information (the information is number of error and answered questions) in my function stat(). But the problem is that in the function val(anv_val) - if the user prints "2", the function game() will run, but I want the stat() to run. I am a little bit confused.



I appreciate all the help!










share|improve this question




















  • 2





    so your problem is because game() returns main() so what happens is as you go through function val() you come to the line res = game() where you return sum_all but then call main() again and so on. You are stuck in a loop. You never get to the line return stat(res) to perform stat(). Since you already return main()in stat() and stat is always called after res = game() you can omit return main() from game()

    – Hadi Farah
    Nov 26 '18 at 9:34













  • Yes but I just want to take the returned value in question(quest, solu), the variable a_f_s and use it in stat() because there I have the information about how many times the player have answered the questions, for instance

    – Gringo
    Nov 26 '18 at 9:38











  • try return sum_all instead of return sum_all, main() in game(). Because you are never reaching stat(). So you are not calling it!

    – Hadi Farah
    Nov 26 '18 at 9:43


















0















I am making a game where the user should write answer (the question() function). In the question function I am using variables of 1 and 0 to get the information if the user's answer is wrong or correct. I am also using variables of 1 and 0 to see if the user has answered the questions.



def main():
menu()
anv_val = ber_val()
val(anv_val)

def menu():
print("1. game")
print("2. stat")
print("3. end")

def ber_val():
val = input("Your choice: ")
while val not in ["1", "2", "3"]:
print("Print 1, 2 or 3.")
val = input("Your choice: ")
return val

def val(anv_val):
if (anv_val == "1"):
res = game()
return res
elif (anv_val == "2"):
res = game()
return stat(res)
else:
return end()

def question(quest, solu):
print(quest)
answer = input("Your answer: ")
a_s = 1
a_f = 0
while (answer != solu):
a_f = 1
print("Try again")
answer = input("Your answer: ")
print("Correct!")
a_f_s = [a_f, a_s]
return a_f_s

def game():
a_1 = question("Your name?", "Ricky")
a_2 = question("Your name?", "Rong")
a_3 = question("Your name?", "Bolly")
sum_f = a_1[0] + a_2[0] + a_3[0]
sum_s = a_1[1] + a_2[1] + a_3[1]
sum_all = [sum_f, sum_s]
return sum_all, main()

def stat(res):
print("Questions you answered: " + str(res[1]))
print("Wrong times: " + str(res[0]))
return main()

main()


Now to my problem. In the function game(), I want to return the variable called a_f_s, AND USE the information (the information is number of error and answered questions) in my function stat(). But the problem is that in the function val(anv_val) - if the user prints "2", the function game() will run, but I want the stat() to run. I am a little bit confused.



I appreciate all the help!










share|improve this question




















  • 2





    so your problem is because game() returns main() so what happens is as you go through function val() you come to the line res = game() where you return sum_all but then call main() again and so on. You are stuck in a loop. You never get to the line return stat(res) to perform stat(). Since you already return main()in stat() and stat is always called after res = game() you can omit return main() from game()

    – Hadi Farah
    Nov 26 '18 at 9:34













  • Yes but I just want to take the returned value in question(quest, solu), the variable a_f_s and use it in stat() because there I have the information about how many times the player have answered the questions, for instance

    – Gringo
    Nov 26 '18 at 9:38











  • try return sum_all instead of return sum_all, main() in game(). Because you are never reaching stat(). So you are not calling it!

    – Hadi Farah
    Nov 26 '18 at 9:43
















0












0








0








I am making a game where the user should write answer (the question() function). In the question function I am using variables of 1 and 0 to get the information if the user's answer is wrong or correct. I am also using variables of 1 and 0 to see if the user has answered the questions.



def main():
menu()
anv_val = ber_val()
val(anv_val)

def menu():
print("1. game")
print("2. stat")
print("3. end")

def ber_val():
val = input("Your choice: ")
while val not in ["1", "2", "3"]:
print("Print 1, 2 or 3.")
val = input("Your choice: ")
return val

def val(anv_val):
if (anv_val == "1"):
res = game()
return res
elif (anv_val == "2"):
res = game()
return stat(res)
else:
return end()

def question(quest, solu):
print(quest)
answer = input("Your answer: ")
a_s = 1
a_f = 0
while (answer != solu):
a_f = 1
print("Try again")
answer = input("Your answer: ")
print("Correct!")
a_f_s = [a_f, a_s]
return a_f_s

def game():
a_1 = question("Your name?", "Ricky")
a_2 = question("Your name?", "Rong")
a_3 = question("Your name?", "Bolly")
sum_f = a_1[0] + a_2[0] + a_3[0]
sum_s = a_1[1] + a_2[1] + a_3[1]
sum_all = [sum_f, sum_s]
return sum_all, main()

def stat(res):
print("Questions you answered: " + str(res[1]))
print("Wrong times: " + str(res[0]))
return main()

main()


Now to my problem. In the function game(), I want to return the variable called a_f_s, AND USE the information (the information is number of error and answered questions) in my function stat(). But the problem is that in the function val(anv_val) - if the user prints "2", the function game() will run, but I want the stat() to run. I am a little bit confused.



I appreciate all the help!










share|improve this question
















I am making a game where the user should write answer (the question() function). In the question function I am using variables of 1 and 0 to get the information if the user's answer is wrong or correct. I am also using variables of 1 and 0 to see if the user has answered the questions.



def main():
menu()
anv_val = ber_val()
val(anv_val)

def menu():
print("1. game")
print("2. stat")
print("3. end")

def ber_val():
val = input("Your choice: ")
while val not in ["1", "2", "3"]:
print("Print 1, 2 or 3.")
val = input("Your choice: ")
return val

def val(anv_val):
if (anv_val == "1"):
res = game()
return res
elif (anv_val == "2"):
res = game()
return stat(res)
else:
return end()

def question(quest, solu):
print(quest)
answer = input("Your answer: ")
a_s = 1
a_f = 0
while (answer != solu):
a_f = 1
print("Try again")
answer = input("Your answer: ")
print("Correct!")
a_f_s = [a_f, a_s]
return a_f_s

def game():
a_1 = question("Your name?", "Ricky")
a_2 = question("Your name?", "Rong")
a_3 = question("Your name?", "Bolly")
sum_f = a_1[0] + a_2[0] + a_3[0]
sum_s = a_1[1] + a_2[1] + a_3[1]
sum_all = [sum_f, sum_s]
return sum_all, main()

def stat(res):
print("Questions you answered: " + str(res[1]))
print("Wrong times: " + str(res[0]))
return main()

main()


Now to my problem. In the function game(), I want to return the variable called a_f_s, AND USE the information (the information is number of error and answered questions) in my function stat(). But the problem is that in the function val(anv_val) - if the user prints "2", the function game() will run, but I want the stat() to run. I am a little bit confused.



I appreciate all the help!







python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 9:29









Micha Wiedenmann

10.5k1364106




10.5k1364106










asked Nov 26 '18 at 9:27









GringoGringo

137




137








  • 2





    so your problem is because game() returns main() so what happens is as you go through function val() you come to the line res = game() where you return sum_all but then call main() again and so on. You are stuck in a loop. You never get to the line return stat(res) to perform stat(). Since you already return main()in stat() and stat is always called after res = game() you can omit return main() from game()

    – Hadi Farah
    Nov 26 '18 at 9:34













  • Yes but I just want to take the returned value in question(quest, solu), the variable a_f_s and use it in stat() because there I have the information about how many times the player have answered the questions, for instance

    – Gringo
    Nov 26 '18 at 9:38











  • try return sum_all instead of return sum_all, main() in game(). Because you are never reaching stat(). So you are not calling it!

    – Hadi Farah
    Nov 26 '18 at 9:43
















  • 2





    so your problem is because game() returns main() so what happens is as you go through function val() you come to the line res = game() where you return sum_all but then call main() again and so on. You are stuck in a loop. You never get to the line return stat(res) to perform stat(). Since you already return main()in stat() and stat is always called after res = game() you can omit return main() from game()

    – Hadi Farah
    Nov 26 '18 at 9:34













  • Yes but I just want to take the returned value in question(quest, solu), the variable a_f_s and use it in stat() because there I have the information about how many times the player have answered the questions, for instance

    – Gringo
    Nov 26 '18 at 9:38











  • try return sum_all instead of return sum_all, main() in game(). Because you are never reaching stat(). So you are not calling it!

    – Hadi Farah
    Nov 26 '18 at 9:43










2




2





so your problem is because game() returns main() so what happens is as you go through function val() you come to the line res = game() where you return sum_all but then call main() again and so on. You are stuck in a loop. You never get to the line return stat(res) to perform stat(). Since you already return main()in stat() and stat is always called after res = game() you can omit return main() from game()

– Hadi Farah
Nov 26 '18 at 9:34







so your problem is because game() returns main() so what happens is as you go through function val() you come to the line res = game() where you return sum_all but then call main() again and so on. You are stuck in a loop. You never get to the line return stat(res) to perform stat(). Since you already return main()in stat() and stat is always called after res = game() you can omit return main() from game()

– Hadi Farah
Nov 26 '18 at 9:34















Yes but I just want to take the returned value in question(quest, solu), the variable a_f_s and use it in stat() because there I have the information about how many times the player have answered the questions, for instance

– Gringo
Nov 26 '18 at 9:38





Yes but I just want to take the returned value in question(quest, solu), the variable a_f_s and use it in stat() because there I have the information about how many times the player have answered the questions, for instance

– Gringo
Nov 26 '18 at 9:38













try return sum_all instead of return sum_all, main() in game(). Because you are never reaching stat(). So you are not calling it!

– Hadi Farah
Nov 26 '18 at 9:43







try return sum_all instead of return sum_all, main() in game(). Because you are never reaching stat(). So you are not calling it!

– Hadi Farah
Nov 26 '18 at 9:43














2 Answers
2






active

oldest

votes


















2














I've added a global variable end_game, to control a while loop in main(). This way, you won't need to keep calling game(), thus simplifying your program logic. Notice that game() does not return a call to main() now.



I also made res a global variable that is only modified when game() is called. In stat(), the global res is used to print stats if it is not None.



end_game = False
res = None

def main():
while not end_game:
menu()
anv_val = ber_val()
val(anv_val)

def menu():
print("1. game")
print("2. stat")
print("3. end")

def ber_val():
val = input("Your choice: ")
while val not in ["1", "2", "3"]:
print("Print 1, 2 or 3.")
val = input("Your choice: ")
return val

def val(anv_val):
global end_game, res
if (anv_val == "1"):
res = game()
elif (anv_val == "2"):
stat()
else:
end_game = True


def question(quest, solu):
print(quest)
answer = input("Your answer: ")
a_s = 1
a_f = 0
while (answer != solu):
a_f = 1
print("Try again")
answer = input("Your answer: ")
print("Correct!")
a_f_s = [a_f, a_s]
return a_f_s

def game():
a_1 = question("Your name?", "Ricky")
a_2 = question("Your name?", "Rong")
a_3 = question("Your name?", "Bolly")
sum_f = a_1[0] + a_2[0] + a_3[0]
sum_s = a_1[1] + a_2[1] + a_3[1]
sum_all = [sum_f, sum_s]
return sum_all

def stat():
if res is not None:
print("Questions you answered: " + str(res[1]))
print("Wrong times: " + str(res[0]))
return

main()


As a bonus, here is a simpler way I would write your code:



def main():
res = None

while True:
print_menu()
choice = get_choice()

if (choice == "1"):
res = game()
elif (choice == "2"):
stat()
else:
return

def print_menu():
print("1. game")
print("2. stat")
print("3. end")

def get_choice():
val = input("Your choice: ")
while val not in ["1", "2", "3"]:
print("Print 1, 2 or 3.")
val = input("Your choice: ")
return val

def question(quest, solu):
print(quest)
answer = input("Your answer: ")
a_s = 1
a_f = 0
while (answer != solu):
a_f = 1
print("Try again")
answer = input("Your answer: ")
print("Correct!")
a_f_s = [a_f, a_s]
return a_f_s

def game():
a_1 = question("Your name?", "Ricky")
a_2 = question("Your name?", "Rong")
a_3 = question("Your name?", "Bolly")
sum_f = a_1[0] + a_2[0] + a_3[0]
sum_s = a_1[1] + a_2[1] + a_3[1]
sum_all = [sum_f, sum_s]
return sum_all

def stat():
if res is not None:
print("Questions you answered: " + str(res[1]))
print("Wrong times: " + str(res[0]))
return

main()





share|improve this answer


























  • I get it! But when I run it - when the player has played the game, I want the program to go back to menu, and when you click on stat - then the statistic should pop up. With this code everything pop up directly

    – Gringo
    Nov 26 '18 at 9:53






  • 1





    @Gringo then don't call res = game() in val(). Instead make a res variable in main() or val() outside the conditions which you assign if 1 is called and then pass to stat() when 2 is called. Here everything pops up directly not because of the answer but because of your code design.

    – Hadi Farah
    Nov 26 '18 at 10:00











  • @HadiFarah is right. I've edited the code since your request is simple. As always, keep your program logically simple and you should be able to track what you need to do

    – bunbun
    Nov 26 '18 at 15:38











  • I've tried in that way too but it says: "local variable 'res' referenced before assignment"

    – Gringo
    Nov 26 '18 at 16:46











  • Check out the new code @Gringo

    – bunbun
    Nov 26 '18 at 17:18



















1














Two observations :





  1. Questions you answered: will always print the same result for all as the program does not proceed until the user has answered all questions.

  2. You should use a_f = a_f + 1 to increment the count of false answers.






share|improve this answer
























  • I know, but that is not my point. I want the to use the a_f_s in the stat() function without running the game() function

    – Gringo
    Nov 26 '18 at 9:40











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53478076%2ffunction-and-parameters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














I've added a global variable end_game, to control a while loop in main(). This way, you won't need to keep calling game(), thus simplifying your program logic. Notice that game() does not return a call to main() now.



I also made res a global variable that is only modified when game() is called. In stat(), the global res is used to print stats if it is not None.



end_game = False
res = None

def main():
while not end_game:
menu()
anv_val = ber_val()
val(anv_val)

def menu():
print("1. game")
print("2. stat")
print("3. end")

def ber_val():
val = input("Your choice: ")
while val not in ["1", "2", "3"]:
print("Print 1, 2 or 3.")
val = input("Your choice: ")
return val

def val(anv_val):
global end_game, res
if (anv_val == "1"):
res = game()
elif (anv_val == "2"):
stat()
else:
end_game = True


def question(quest, solu):
print(quest)
answer = input("Your answer: ")
a_s = 1
a_f = 0
while (answer != solu):
a_f = 1
print("Try again")
answer = input("Your answer: ")
print("Correct!")
a_f_s = [a_f, a_s]
return a_f_s

def game():
a_1 = question("Your name?", "Ricky")
a_2 = question("Your name?", "Rong")
a_3 = question("Your name?", "Bolly")
sum_f = a_1[0] + a_2[0] + a_3[0]
sum_s = a_1[1] + a_2[1] + a_3[1]
sum_all = [sum_f, sum_s]
return sum_all

def stat():
if res is not None:
print("Questions you answered: " + str(res[1]))
print("Wrong times: " + str(res[0]))
return

main()


As a bonus, here is a simpler way I would write your code:



def main():
res = None

while True:
print_menu()
choice = get_choice()

if (choice == "1"):
res = game()
elif (choice == "2"):
stat()
else:
return

def print_menu():
print("1. game")
print("2. stat")
print("3. end")

def get_choice():
val = input("Your choice: ")
while val not in ["1", "2", "3"]:
print("Print 1, 2 or 3.")
val = input("Your choice: ")
return val

def question(quest, solu):
print(quest)
answer = input("Your answer: ")
a_s = 1
a_f = 0
while (answer != solu):
a_f = 1
print("Try again")
answer = input("Your answer: ")
print("Correct!")
a_f_s = [a_f, a_s]
return a_f_s

def game():
a_1 = question("Your name?", "Ricky")
a_2 = question("Your name?", "Rong")
a_3 = question("Your name?", "Bolly")
sum_f = a_1[0] + a_2[0] + a_3[0]
sum_s = a_1[1] + a_2[1] + a_3[1]
sum_all = [sum_f, sum_s]
return sum_all

def stat():
if res is not None:
print("Questions you answered: " + str(res[1]))
print("Wrong times: " + str(res[0]))
return

main()





share|improve this answer


























  • I get it! But when I run it - when the player has played the game, I want the program to go back to menu, and when you click on stat - then the statistic should pop up. With this code everything pop up directly

    – Gringo
    Nov 26 '18 at 9:53






  • 1





    @Gringo then don't call res = game() in val(). Instead make a res variable in main() or val() outside the conditions which you assign if 1 is called and then pass to stat() when 2 is called. Here everything pops up directly not because of the answer but because of your code design.

    – Hadi Farah
    Nov 26 '18 at 10:00











  • @HadiFarah is right. I've edited the code since your request is simple. As always, keep your program logically simple and you should be able to track what you need to do

    – bunbun
    Nov 26 '18 at 15:38











  • I've tried in that way too but it says: "local variable 'res' referenced before assignment"

    – Gringo
    Nov 26 '18 at 16:46











  • Check out the new code @Gringo

    – bunbun
    Nov 26 '18 at 17:18
















2














I've added a global variable end_game, to control a while loop in main(). This way, you won't need to keep calling game(), thus simplifying your program logic. Notice that game() does not return a call to main() now.



I also made res a global variable that is only modified when game() is called. In stat(), the global res is used to print stats if it is not None.



end_game = False
res = None

def main():
while not end_game:
menu()
anv_val = ber_val()
val(anv_val)

def menu():
print("1. game")
print("2. stat")
print("3. end")

def ber_val():
val = input("Your choice: ")
while val not in ["1", "2", "3"]:
print("Print 1, 2 or 3.")
val = input("Your choice: ")
return val

def val(anv_val):
global end_game, res
if (anv_val == "1"):
res = game()
elif (anv_val == "2"):
stat()
else:
end_game = True


def question(quest, solu):
print(quest)
answer = input("Your answer: ")
a_s = 1
a_f = 0
while (answer != solu):
a_f = 1
print("Try again")
answer = input("Your answer: ")
print("Correct!")
a_f_s = [a_f, a_s]
return a_f_s

def game():
a_1 = question("Your name?", "Ricky")
a_2 = question("Your name?", "Rong")
a_3 = question("Your name?", "Bolly")
sum_f = a_1[0] + a_2[0] + a_3[0]
sum_s = a_1[1] + a_2[1] + a_3[1]
sum_all = [sum_f, sum_s]
return sum_all

def stat():
if res is not None:
print("Questions you answered: " + str(res[1]))
print("Wrong times: " + str(res[0]))
return

main()


As a bonus, here is a simpler way I would write your code:



def main():
res = None

while True:
print_menu()
choice = get_choice()

if (choice == "1"):
res = game()
elif (choice == "2"):
stat()
else:
return

def print_menu():
print("1. game")
print("2. stat")
print("3. end")

def get_choice():
val = input("Your choice: ")
while val not in ["1", "2", "3"]:
print("Print 1, 2 or 3.")
val = input("Your choice: ")
return val

def question(quest, solu):
print(quest)
answer = input("Your answer: ")
a_s = 1
a_f = 0
while (answer != solu):
a_f = 1
print("Try again")
answer = input("Your answer: ")
print("Correct!")
a_f_s = [a_f, a_s]
return a_f_s

def game():
a_1 = question("Your name?", "Ricky")
a_2 = question("Your name?", "Rong")
a_3 = question("Your name?", "Bolly")
sum_f = a_1[0] + a_2[0] + a_3[0]
sum_s = a_1[1] + a_2[1] + a_3[1]
sum_all = [sum_f, sum_s]
return sum_all

def stat():
if res is not None:
print("Questions you answered: " + str(res[1]))
print("Wrong times: " + str(res[0]))
return

main()





share|improve this answer


























  • I get it! But when I run it - when the player has played the game, I want the program to go back to menu, and when you click on stat - then the statistic should pop up. With this code everything pop up directly

    – Gringo
    Nov 26 '18 at 9:53






  • 1





    @Gringo then don't call res = game() in val(). Instead make a res variable in main() or val() outside the conditions which you assign if 1 is called and then pass to stat() when 2 is called. Here everything pops up directly not because of the answer but because of your code design.

    – Hadi Farah
    Nov 26 '18 at 10:00











  • @HadiFarah is right. I've edited the code since your request is simple. As always, keep your program logically simple and you should be able to track what you need to do

    – bunbun
    Nov 26 '18 at 15:38











  • I've tried in that way too but it says: "local variable 'res' referenced before assignment"

    – Gringo
    Nov 26 '18 at 16:46











  • Check out the new code @Gringo

    – bunbun
    Nov 26 '18 at 17:18














2












2








2







I've added a global variable end_game, to control a while loop in main(). This way, you won't need to keep calling game(), thus simplifying your program logic. Notice that game() does not return a call to main() now.



I also made res a global variable that is only modified when game() is called. In stat(), the global res is used to print stats if it is not None.



end_game = False
res = None

def main():
while not end_game:
menu()
anv_val = ber_val()
val(anv_val)

def menu():
print("1. game")
print("2. stat")
print("3. end")

def ber_val():
val = input("Your choice: ")
while val not in ["1", "2", "3"]:
print("Print 1, 2 or 3.")
val = input("Your choice: ")
return val

def val(anv_val):
global end_game, res
if (anv_val == "1"):
res = game()
elif (anv_val == "2"):
stat()
else:
end_game = True


def question(quest, solu):
print(quest)
answer = input("Your answer: ")
a_s = 1
a_f = 0
while (answer != solu):
a_f = 1
print("Try again")
answer = input("Your answer: ")
print("Correct!")
a_f_s = [a_f, a_s]
return a_f_s

def game():
a_1 = question("Your name?", "Ricky")
a_2 = question("Your name?", "Rong")
a_3 = question("Your name?", "Bolly")
sum_f = a_1[0] + a_2[0] + a_3[0]
sum_s = a_1[1] + a_2[1] + a_3[1]
sum_all = [sum_f, sum_s]
return sum_all

def stat():
if res is not None:
print("Questions you answered: " + str(res[1]))
print("Wrong times: " + str(res[0]))
return

main()


As a bonus, here is a simpler way I would write your code:



def main():
res = None

while True:
print_menu()
choice = get_choice()

if (choice == "1"):
res = game()
elif (choice == "2"):
stat()
else:
return

def print_menu():
print("1. game")
print("2. stat")
print("3. end")

def get_choice():
val = input("Your choice: ")
while val not in ["1", "2", "3"]:
print("Print 1, 2 or 3.")
val = input("Your choice: ")
return val

def question(quest, solu):
print(quest)
answer = input("Your answer: ")
a_s = 1
a_f = 0
while (answer != solu):
a_f = 1
print("Try again")
answer = input("Your answer: ")
print("Correct!")
a_f_s = [a_f, a_s]
return a_f_s

def game():
a_1 = question("Your name?", "Ricky")
a_2 = question("Your name?", "Rong")
a_3 = question("Your name?", "Bolly")
sum_f = a_1[0] + a_2[0] + a_3[0]
sum_s = a_1[1] + a_2[1] + a_3[1]
sum_all = [sum_f, sum_s]
return sum_all

def stat():
if res is not None:
print("Questions you answered: " + str(res[1]))
print("Wrong times: " + str(res[0]))
return

main()





share|improve this answer















I've added a global variable end_game, to control a while loop in main(). This way, you won't need to keep calling game(), thus simplifying your program logic. Notice that game() does not return a call to main() now.



I also made res a global variable that is only modified when game() is called. In stat(), the global res is used to print stats if it is not None.



end_game = False
res = None

def main():
while not end_game:
menu()
anv_val = ber_val()
val(anv_val)

def menu():
print("1. game")
print("2. stat")
print("3. end")

def ber_val():
val = input("Your choice: ")
while val not in ["1", "2", "3"]:
print("Print 1, 2 or 3.")
val = input("Your choice: ")
return val

def val(anv_val):
global end_game, res
if (anv_val == "1"):
res = game()
elif (anv_val == "2"):
stat()
else:
end_game = True


def question(quest, solu):
print(quest)
answer = input("Your answer: ")
a_s = 1
a_f = 0
while (answer != solu):
a_f = 1
print("Try again")
answer = input("Your answer: ")
print("Correct!")
a_f_s = [a_f, a_s]
return a_f_s

def game():
a_1 = question("Your name?", "Ricky")
a_2 = question("Your name?", "Rong")
a_3 = question("Your name?", "Bolly")
sum_f = a_1[0] + a_2[0] + a_3[0]
sum_s = a_1[1] + a_2[1] + a_3[1]
sum_all = [sum_f, sum_s]
return sum_all

def stat():
if res is not None:
print("Questions you answered: " + str(res[1]))
print("Wrong times: " + str(res[0]))
return

main()


As a bonus, here is a simpler way I would write your code:



def main():
res = None

while True:
print_menu()
choice = get_choice()

if (choice == "1"):
res = game()
elif (choice == "2"):
stat()
else:
return

def print_menu():
print("1. game")
print("2. stat")
print("3. end")

def get_choice():
val = input("Your choice: ")
while val not in ["1", "2", "3"]:
print("Print 1, 2 or 3.")
val = input("Your choice: ")
return val

def question(quest, solu):
print(quest)
answer = input("Your answer: ")
a_s = 1
a_f = 0
while (answer != solu):
a_f = 1
print("Try again")
answer = input("Your answer: ")
print("Correct!")
a_f_s = [a_f, a_s]
return a_f_s

def game():
a_1 = question("Your name?", "Ricky")
a_2 = question("Your name?", "Rong")
a_3 = question("Your name?", "Bolly")
sum_f = a_1[0] + a_2[0] + a_3[0]
sum_s = a_1[1] + a_2[1] + a_3[1]
sum_all = [sum_f, sum_s]
return sum_all

def stat():
if res is not None:
print("Questions you answered: " + str(res[1]))
print("Wrong times: " + str(res[0]))
return

main()






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 17:17

























answered Nov 26 '18 at 9:46









bunbunbunbun

2,08532449




2,08532449













  • I get it! But when I run it - when the player has played the game, I want the program to go back to menu, and when you click on stat - then the statistic should pop up. With this code everything pop up directly

    – Gringo
    Nov 26 '18 at 9:53






  • 1





    @Gringo then don't call res = game() in val(). Instead make a res variable in main() or val() outside the conditions which you assign if 1 is called and then pass to stat() when 2 is called. Here everything pops up directly not because of the answer but because of your code design.

    – Hadi Farah
    Nov 26 '18 at 10:00











  • @HadiFarah is right. I've edited the code since your request is simple. As always, keep your program logically simple and you should be able to track what you need to do

    – bunbun
    Nov 26 '18 at 15:38











  • I've tried in that way too but it says: "local variable 'res' referenced before assignment"

    – Gringo
    Nov 26 '18 at 16:46











  • Check out the new code @Gringo

    – bunbun
    Nov 26 '18 at 17:18



















  • I get it! But when I run it - when the player has played the game, I want the program to go back to menu, and when you click on stat - then the statistic should pop up. With this code everything pop up directly

    – Gringo
    Nov 26 '18 at 9:53






  • 1





    @Gringo then don't call res = game() in val(). Instead make a res variable in main() or val() outside the conditions which you assign if 1 is called and then pass to stat() when 2 is called. Here everything pops up directly not because of the answer but because of your code design.

    – Hadi Farah
    Nov 26 '18 at 10:00











  • @HadiFarah is right. I've edited the code since your request is simple. As always, keep your program logically simple and you should be able to track what you need to do

    – bunbun
    Nov 26 '18 at 15:38











  • I've tried in that way too but it says: "local variable 'res' referenced before assignment"

    – Gringo
    Nov 26 '18 at 16:46











  • Check out the new code @Gringo

    – bunbun
    Nov 26 '18 at 17:18

















I get it! But when I run it - when the player has played the game, I want the program to go back to menu, and when you click on stat - then the statistic should pop up. With this code everything pop up directly

– Gringo
Nov 26 '18 at 9:53





I get it! But when I run it - when the player has played the game, I want the program to go back to menu, and when you click on stat - then the statistic should pop up. With this code everything pop up directly

– Gringo
Nov 26 '18 at 9:53




1




1





@Gringo then don't call res = game() in val(). Instead make a res variable in main() or val() outside the conditions which you assign if 1 is called and then pass to stat() when 2 is called. Here everything pops up directly not because of the answer but because of your code design.

– Hadi Farah
Nov 26 '18 at 10:00





@Gringo then don't call res = game() in val(). Instead make a res variable in main() or val() outside the conditions which you assign if 1 is called and then pass to stat() when 2 is called. Here everything pops up directly not because of the answer but because of your code design.

– Hadi Farah
Nov 26 '18 at 10:00













@HadiFarah is right. I've edited the code since your request is simple. As always, keep your program logically simple and you should be able to track what you need to do

– bunbun
Nov 26 '18 at 15:38





@HadiFarah is right. I've edited the code since your request is simple. As always, keep your program logically simple and you should be able to track what you need to do

– bunbun
Nov 26 '18 at 15:38













I've tried in that way too but it says: "local variable 'res' referenced before assignment"

– Gringo
Nov 26 '18 at 16:46





I've tried in that way too but it says: "local variable 'res' referenced before assignment"

– Gringo
Nov 26 '18 at 16:46













Check out the new code @Gringo

– bunbun
Nov 26 '18 at 17:18





Check out the new code @Gringo

– bunbun
Nov 26 '18 at 17:18













1














Two observations :





  1. Questions you answered: will always print the same result for all as the program does not proceed until the user has answered all questions.

  2. You should use a_f = a_f + 1 to increment the count of false answers.






share|improve this answer
























  • I know, but that is not my point. I want the to use the a_f_s in the stat() function without running the game() function

    – Gringo
    Nov 26 '18 at 9:40
















1














Two observations :





  1. Questions you answered: will always print the same result for all as the program does not proceed until the user has answered all questions.

  2. You should use a_f = a_f + 1 to increment the count of false answers.






share|improve this answer
























  • I know, but that is not my point. I want the to use the a_f_s in the stat() function without running the game() function

    – Gringo
    Nov 26 '18 at 9:40














1












1








1







Two observations :





  1. Questions you answered: will always print the same result for all as the program does not proceed until the user has answered all questions.

  2. You should use a_f = a_f + 1 to increment the count of false answers.






share|improve this answer













Two observations :





  1. Questions you answered: will always print the same result for all as the program does not proceed until the user has answered all questions.

  2. You should use a_f = a_f + 1 to increment the count of false answers.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 9:36









GautamGautam

1,12329




1,12329













  • I know, but that is not my point. I want the to use the a_f_s in the stat() function without running the game() function

    – Gringo
    Nov 26 '18 at 9:40



















  • I know, but that is not my point. I want the to use the a_f_s in the stat() function without running the game() function

    – Gringo
    Nov 26 '18 at 9:40

















I know, but that is not my point. I want the to use the a_f_s in the stat() function without running the game() function

– Gringo
Nov 26 '18 at 9:40





I know, but that is not my point. I want the to use the a_f_s in the stat() function without running the game() function

– Gringo
Nov 26 '18 at 9:40


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53478076%2ffunction-and-parameters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno