gpt4 book ai didi

Python代码不会添加到变量

转载 作者:行者123 更新时间:2023-11-30 22:43:00 25 4
gpt4 key购买 nike

我已经为一个项目的非常基本的测验编写了一些代码,但是无论我做什么,程序总是输出我在开始时设置的变量。它似乎选择了正确的输入,并且 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()

最佳答案

您的问题是您错误地使用了 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 作为参数传递给函数,因此您还应该将其从函数调用中删除。

关于Python代码不会添加到变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41922681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com