1

I've written some code for a very basic quiz for a project, but whatever I do the program always outputs what I've set the variable to at the start. It seems to be choosing the right input, and the else statements work, so I can only assume I've done something wrong in the adding to "per" but I can't work out what Here's the code...

from random import randint
print(" How like xx are you? Take a test!")
print(" whats your name?")
appendMe = str(input())
input(" press enter to start")
global per
def T(per , a , b , qu):
   per = (per + 0 )
   print(" Input the letter of the answer you choose !")
   print ( qu )
   print ( a )
   print ( b )
   choice = str(input())
   if choice == "A" :
       per = int(per + 10)
   elif choice == "B" :
       per = int(per)
   else:
       print(" Input either A , B , C or D ... Lets try again...")
       Q(per , a , b ,c ,d)



def Q(per , a , b ,c ,d):
       per = (per + 0 )
       cho = int(randint(0,2))
       if cho == 1 :
        print(" Input the letter of the answer you choose !")
        print ( qu )
        print ( a )
        print ( b )
        print ( c )
        print ( d )
        choice = str(input())
        if choice == "A" :
            per = int(per + 5)
        elif choice == "B" :
            per = int(per + 2)
        elif choice == "C" :
             per = int(per)
        elif choice == "D" :
                    per = int(per )
        else:
             print(" Input either A , B , C or D ... Lets try again...")
             Q(per , a , b ,c ,d)
    else:
        print(" Input the letter of the answer you choose !")
        print ( qu )
        print ( d )
        print ( c )
        print ( b )
        print ( a )
        choice = str(input())
        if choice == "D" :
            per = int(per + 5)
        elif choice == "C" :
            per = int(per + 2)
        elif choice == "B" :
            per = int(per )
        elif choice == "A" :
            per = int(per)
        else:
            print(" Input either A , B , C or D ... Lets try again...")
            Q(per , a , b ,c ,d)
    

per = int(50)                
qu = " What is/was/will be your favourite subject at school"
a = " Computer science "
b = " English"
c = " art"
d = " textiles"

print("You are " , per , "percent xx!")
appendFile = open(" exampleFile.txt" , "a")
appendFile.write("\n")
appendFile.write(appendMe )
appendFile.write(per)
appendFile.close()
3
  • Didn't read the code properly, but is it because of the local variable per in both functions? try changing the name. Commented Jan 29, 2017 at 15:43
  • 2
    global should be placed withing the functions, and you should not pass it as a parameter... Commented Jan 29, 2017 at 15:43
  • I get an indentation error when I run the code pasted as is. Commented Jan 29, 2017 at 15:43

2 Answers 2

3

Your issue is that you are mis-using the global statement. It should be inside a function, so your functions become

def T(a , b , qu):
    global per
    per = (per + 0 )
    etc...

def Q(a , b ,c ,d):
   global per
   per = (per + 0 )
   etc...

Edit: You now no longer need to pass per as an argument to the function, so you should also remove it from your function calls.

Sign up to request clarification or add additional context in comments.

3 Comments

Why do you need to pass per as parameter if you use global ?
I've changed the global to where you suggested , inside the function. But it still won't work, the weird thing is that when I set per to 20 or any other number at the start it still prints it out as 50!
Try fixing your indentation. Keep it regular, something like four spaces or one tab per indent, and run again.
0

You should write a function, that returns the change in points and don't actually try to change the points.

from random import shuffle

def ask(question, answers):
    print("Input the letter of the answer you choose !")
    random.shuffle(answers)
    all_points = {}
    for letter, (answer, points) in zip("ABCDEFG", answers):
        print("{}: {}".format(letter, answer)
        all_points[letter] = points
    while True:
        choice = input()
        if choice in all_points:
            break
        print("Input either A , B , C or D ... Lets try again...")
    return all_points[choice]

per = 50 
per += ask("What is/was/will be your favourite subject at school", [
    (" Computer science ", 5),
    (" English", 2),
    (" art", 0),
    (" textiles", 0)])
per += ask(" What are your beliefs on body and soul?", [
    (" I'm pretty sure the soul dosen't exist...", 5),
    (" Whats a soul?", 2),
    (" I agree with plato , The soul exist and its one with the body", 0),
    (" I agree WTH", 0)])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.