Function and Parameters
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
add a comment |
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
2
so your problem is becausegame()returnsmain()so what happens is as you go through functionval()you come to the lineres = game()where you returnsum_allbut then callmain()again and so on. You are stuck in a loop. You never get to the linereturn stat(res)to performstat(). Since you alreadyreturn main()instat()and stat is always called afterres = game()you can omitreturn main()fromgame()
– 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
tryreturn sum_allinstead ofreturn sum_all, main()ingame(). Because you are never reachingstat(). So you are not calling it!
– Hadi Farah
Nov 26 '18 at 9:43
add a comment |
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
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
python python-3.x
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 becausegame()returnsmain()so what happens is as you go through functionval()you come to the lineres = game()where you returnsum_allbut then callmain()again and so on. You are stuck in a loop. You never get to the linereturn stat(res)to performstat(). Since you alreadyreturn main()instat()and stat is always called afterres = game()you can omitreturn main()fromgame()
– 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
tryreturn sum_allinstead ofreturn sum_all, main()ingame(). Because you are never reachingstat(). So you are not calling it!
– Hadi Farah
Nov 26 '18 at 9:43
add a comment |
2
so your problem is becausegame()returnsmain()so what happens is as you go through functionval()you come to the lineres = game()where you returnsum_allbut then callmain()again and so on. You are stuck in a loop. You never get to the linereturn stat(res)to performstat(). Since you alreadyreturn main()instat()and stat is always called afterres = game()you can omitreturn main()fromgame()
– 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
tryreturn sum_allinstead ofreturn sum_all, main()ingame(). Because you are never reachingstat(). 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
add a comment |
2 Answers
2
active
oldest
votes
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()
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 callres = game()inval(). Instead make aresvariable inmain()orval()outside the conditions which you assign if1is called and then pass tostat()when2is 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
|
show 4 more comments
Two observations :
Questions you answered:will always print the same result for all as the program does not proceed until the user has answered all questions.- You should use
a_f = a_f + 1to increment the count of false answers.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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()
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 callres = game()inval(). Instead make aresvariable inmain()orval()outside the conditions which you assign if1is called and then pass tostat()when2is 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
|
show 4 more comments
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()
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 callres = game()inval(). Instead make aresvariable inmain()orval()outside the conditions which you assign if1is called and then pass tostat()when2is 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
|
show 4 more comments
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()
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()
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 callres = game()inval(). Instead make aresvariable inmain()orval()outside the conditions which you assign if1is called and then pass tostat()when2is 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
|
show 4 more comments
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 callres = game()inval(). Instead make aresvariable inmain()orval()outside the conditions which you assign if1is called and then pass tostat()when2is 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
|
show 4 more comments
Two observations :
Questions you answered:will always print the same result for all as the program does not proceed until the user has answered all questions.- You should use
a_f = a_f + 1to increment the count of false answers.
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
add a comment |
Two observations :
Questions you answered:will always print the same result for all as the program does not proceed until the user has answered all questions.- You should use
a_f = a_f + 1to increment the count of false answers.
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
add a comment |
Two observations :
Questions you answered:will always print the same result for all as the program does not proceed until the user has answered all questions.- You should use
a_f = a_f + 1to increment the count of false answers.
Two observations :
Questions you answered:will always print the same result for all as the program does not proceed until the user has answered all questions.- You should use
a_f = a_f + 1to increment the count of false answers.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
so your problem is because
game()returnsmain()so what happens is as you go through functionval()you come to the lineres = game()where you returnsum_allbut then callmain()again and so on. You are stuck in a loop. You never get to the linereturn stat(res)to performstat(). Since you alreadyreturn main()instat()and stat is always called afterres = game()you can omitreturn main()fromgame()– 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_allinstead ofreturn sum_all, main()ingame(). Because you are never reachingstat(). So you are not calling it!– Hadi Farah
Nov 26 '18 at 9:43