Python代码不会添加到变量中

时间:2017-01-29 15:41:29

标签: python

我为一个项目的一个非常基本的测验编写了一些代码,但无论我做什么,程序总是输出我在开始时设置变量的内容。它似乎是选择正确的输入,而else语句是有效的,所以我只能假设我在添加到“per”时做错了什么但我无法弄清楚这是什么代码......

from random import randint
print(" How like solly 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"
Q(per , a , b ,c ,d)
qu = " What are your beliefs on body and soul?"
a = " I'm pretty sure the soul dosen't exist..."
b = " Whats a soul?"
c = " I agree with plato , The soul exist and its one with the body"
d = " I agree WTH" ##########
Q(per , a , b ,c ,d)
qu = " Which of these football teams do you support?"
a = " Reading FC"
b = " Arsenal"

c= " I dont supprt any"
d = " Man united"
Q(per , a , b ,c ,d)
qu = " Whats your colour?"
a = " pink"
b = " orange "
c = " blue"
d = "black"
Q(per , a , b ,c ,d)
qu = " Whats your favourte musica intrstument "
a = " Guitar"
b = " Drums"
c = " piano"
d = " violin"
Q(per , a , b ,c ,d)
qu = "Which of these your favourite sport?"
a = " tennis "
b = " football"
c = " gymastics"
d  = " netball"
Q(per , a , b ,c ,d)
qu = "Which of these is your favourite food?"
a = " Falafel"
b  = "pizza"
c = " pasta"
d = " A burger"
Q(per , a , b ,c ,d)
qu = " apple or android?"
a = " android "
b = "apple"
T(per , a , b , qu)
qu = " Which on of these is your favourite TV show"
a = " jundge rinder "
b = " sunday poilitics"
c = " the next step "
d = " strictly come dancing"
Q(per , a , b ,c ,d)
qu = " Which type of music is your favorutie "
a = " Pop"
b = " rap"
c = " grime "
d = " classical"
Q(per , a , b ,c ,d)
qu = " Which band is yur favourite?"
a = " One direction"
b = " The vamps"
c = "ACDC"
d = " little mix"
Q(per , a , b ,c ,d)
print("You are " , per , "percent solly!")
appendFile = open(" exampleFile.txt" , "a")
appendFile.write("\n")
appendFile.write(appendMe )
appendFile.write(per)
appendFile.close()

2 个答案:

答案 0 :(得分:3)

您的问题是您使用了global声明。它应该在函数内部,因此您的函数变为

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

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

编辑:您现在不再需要将per作为参数传递给函数,因此您还应该从函数调用中删除它。

答案 1 :(得分:0)

你应该写一个函数,它返回点数的变化而不是真正尝试改变点。

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)])