Riggycat Riggycat - 1 month ago 5
Python Question

Restarting a function in Python 3.4

I need help for my python assignment. We have to make a quiz program and I am having trouble with restarting a function.

I need something like continue, but instead runs the function again. Also, some tips on returning values from functions cant hurt! Thanks! ~Also, I just started using python 2 weeks ago, so this is pretty advanced to me.

#Quiz YAY!
#
#Patrick Leonard
#
#Version 1.0
#
score = 0;
modPassword = "200605015"
def modMode(score):
print("Entering Overide Mode");
print("Opening Overide Console");
cmd = input("Enter Command: ");
if cmd == "corr":
print("Adding one point");
score=score+1
elif cmd== "manScoreChng":
score=int(input("What do want the score to be?"));
else
return int(score);
modMode(score);
print(score);

My result from 2ps's answer
Entering Overide Mode
Opening Overide Console
Enter Command: manScoreChng
What do want the score to be?32
0

2ps 2ps
Answer

To capture the return of the modMode function, just make sure you return something at the end:

score = 0;
modPassword = "200605015"
def modMode(score):
   print("Entering Overide Mode")
   print("Opening Overide Console")
   cmd = input("Enter Command:  ")
   if cmd == "corr":
      print("Adding one point")
      score = score+1
   elif cmd == "manScoreChng":
      score = int(input("What do want the score to be?"))
   elif cmd == 'exit':
      raise Exception('Bye!')
   return int(score)

To call the modScore command over and over again, use a loop.

try:
  while True:
    score = modMode(score) # grab the returned value from modMode by using `=`
    print(score)
except Exception:
  pass

This will run until the user types in exit.

Comments