-2

The example here is a program in Python 3 that helps you memorize the digits of pi. I have tried using the var counter1 to show the current score, while counter2 is the score from the last round. However, I am unable to use an if statement to print how much you have improved from the last round because I am not allowed to reference counter2 after the if statement in my loop. How should this be done? I feel like there is a significant inefficiency in my code, it does not feel right at all. The ct variable was an attempt at only running the if statement if we were past the first round.

def test():
  ans = (input("\nList the digits of pi! Three point... \nPi = 3."))
  if (ans).isdigit() == False:print("Oops! Numbers only!")
  counter1=0
  for i in range(0,(len(ans))):
      if ans[i] == pi[i]:
          counter1+=1
      else: break
  next = pi[counter1:counter1+6]
  if counter1==1: article=""
  else: article="s"
  print("\nYou got {0} digit{1}. \n\n{2}\n{3}".format(counter1, article, "3."+pi[0:counter1], (' '*(counter1+2))+next))
  if counter1!=counter2 and ct!=0:
    print("That's {0} more digits than the last time!".format(counter2-counter1))
  inp=input("Continue? Y/N")
  if inp=="N":
    exit()
  counter2=counter1
  ct+=1
  test()

ct=0
counter2=0
test()
iBug
  • 35,554
  • 7
  • 89
  • 134

2 Answers2

0

What you are trying (to break namespacing) is something exactly prohibited and wrong. Also you are using wrong words when you say you want to access variables from last iteration, it's in fact you want to access variables from last function run.

There is no way to do this unless you pass in the previous value in your function like

def test(prev_value):
    ...
    ...

if not val:
    val = 0
val = test(prev_value=val)
0xc0de
  • 8,028
  • 5
  • 49
  • 75
0

I think your code does not work properly. Here is a modification of yours. I use while for the repeating condition. The function can take inputs, test(vat1, var2, ..).


The code :

import math

pi = str(math.pi);

def test(prev, count):
    ans_pi = input("(Attempt no."+str(count)+") Please input the digits of Pi : 3.");
    if ans_pi.isdigit() != True:
        print("Retry, input must be numbers");
        return test(prev, count);
    else:
        current=0;
        for i in range(len(ans_pi)):
            if ans_pi[i]==pi[2+i]:
                current+=1;
            else:
                break;
        print("\nYou got {} digit(s). \n\n".format(current))
        if current!=prev and count>1: 
            print("That's {} more digit(s) than the last time! \n \n".format(current-prev)) 

        while(input("Continue? (Y) \n\n").capitalize() == "Y"):
            prev=current;
            count+=1;
            return test(prev, count);
        exit();

prev=0;
test(prev,1)

Example output :

(Attempt no.1) Please input the digits of Pi : 3.145

You got 2 digit(s).


Continue? (Y)

y
(Attempt no.2) Please input the digits of Pi : 3.14156

You got 4 digit(s).


That's 2 more digit(s) than the last time!


Continue? (Y)

n