变量的值不会普遍改变

2024-04-27 00:57:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我对课程非常陌生,想通过编写代码来练习。你知道吗

为什么check_Float的值中的变量p没有被转移到代码底部块中的变量p。你知道吗

while p != 3:
    A.questiondef(p)
    print(p)

由于这个值不变,代码只会问无限多的问题。你知道吗

class B:
    def which_Class(self):
        if i == 0:
            self.class_number = ("Class One")
        elif i == 1:
            self.class_number = ("Class Two")
        elif i == 2:
            self.class_number = ("Class Three")
        else:
            None

    def questiondef(self, p):
        operations = {"x":operator.mul , "-":operator.sub , "/":operator.truediv, "+":operator.add}
        self.num1 = random.randint(1,200)
        self.num2 = random.randint(1,10)
        self.ops = random.choice(list(operations.keys()))
        self.answer = ((operations.get(self.ops))(self.num1, self.num2))
        A.check_Float(p)


    def student_Score(self, score):
        if p == 3:
            print(name ,  "Your score out of 3 is: " , score)
            if i == 0:
                class_1[y][0] = (score)
                class_1[y][1] = (name)
            elif i == 1:
                class_2.append(score)
            elif i == 2:
                class_3.append(score)

            self.score = 0
        else:
            None
    def check_Float(self, p):
        float1 = self.answer%1
        if float1 == 0:
            self.question = int(input("What is {} {} {}?: ".format(self.num1 , self.ops , self.num2)))
            if self.question == self.answer:
                print("\nCorrect\n")
                p = p+1
                return True
            else:
                print("\nIncorrect\n")
                p = p + 1
                return False

        else:
            return False
            A.questiondef(p)

p = 0
class_1 = [["",""],["",""],["",""]]
class_2 = []
class_3 = []
class_number = ""
A = B()
score = 0
import random
import operator
for i in range (3):
    A.which_Class()
    print(A.class_number)
    for y in range(3): #Assuming there are 3 students in a class
        p = 0
        name = input("\nWhat is your name?: " )
        score = 0
        while p != 3:
            A.questiondef(p)
            print(p)

            if p ==3:
                A.student_Score(score)

Tags: 代码nameselfnumberifdefrandomoperator
1条回答
网友
1楼 · 发布于 2024-04-27 00:57:10

您可以使用全局关键字 使用like as 代码顶部p=0 然后在每个函数中,或在其中使用p的函数中 你可以把p变成全局的 函数顶部的全局p

相关问题 更多 >