我对课程很新,并希望通过制作此代码来练习。
为什么p
的值中的变量check_Float
不会传输到代码底部的变量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)
答案 0 :(得分:2)
您可以使用全球关键字 用得像 代码顶部p = 0 然后在每个使用p的函数或函数中 你可以把p全局化为 功能顶部的全局p