Is there any way to make this Python function run for more intervals? -


i extremely new python , wrote simple code. in end made feet wet , head wrapped around coding. python first language, started learning couple days ago.

and yes, aware roundabout choice coding method, it's first thing ever produced myself more couple lines long. so, in end, there way make function question run more times without running issue variables being returned? unsure whether issue or tired see reason right now. know need create more if statements results section. obvious.

name = raw_input("please enter preferred name: ") print "welcome, %r, general stupidity quiz." % name raw_input("\n\tplease press button continue")  question1 = "\n\nwhat equivilent of 2pi in mathmatics? " answer1 = "tao" answer1_5 = "tao" question2 = "\nwhat 2 + 2? " answer2 = "4" w = 0  def question(question, answerq, answere, inputs):     user_answer = raw_input(question)     if user_answer in [str(answerq), str(answere)]:         print "correct!"         x = inputs + 1         return x     else:         print "false!"         x = inputs         return x  x = question(question1, answer1, answer1_5, w) x = question(question2, answer2, answer2, x)  print "\nyou got " + str(x) + " questions correct..." if x == 2:     print "you're not stupid!" elif x == 1:     print "you're not smart!" else:     print "i hate tell this...but..." raw_input() 

i added raw_input() @ end cmd window wouldn't close. know use ubuntu (recently uninstalled it) , run code using cmd window, it's thing tagged onto end.

you should aim have functions self contained , 1 thing. function checks correctness of user input question , increments counter. 2 things. move incrementing of counter out of function:

def question(question, answerq, answere):     user_answer = raw_input(question)     if user_answer in [str(answerq), str(answere)]:         print "correct!"         return true     else:         print "false!"         return false  x = 0 if question(question1, answer1, answer1_5):     x += 1 if question(question2, answer2, answer2):     x += 1 

now going clean little changing names more meaningful , not assuming every question have 2 answers:

def question(question, correct_answers):     user_answer = raw_input(question)     if user_answer in correct_answers:         print "correct!"         return true     else:         print "false!"         return false  correct_count = 0 if question(question1, [answer1, answer1_5]):     correct_count += 1 if question(question2, [answer2, answer2]):     correct_count += 1 

to learn more, coding best practices , coding style. recommend reading pep8 python.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -