当前位置:   article > 正文

python名称定义怎么解决_类的“Python名称错误:未定义名称”

python名称定义怎么解决_类的“Python名称错误:未定义名称”

我很难在Python代码中完全定义Questions类。我玩过它,但一直没有运气。在import sys

import random

class Question():

def __init__(self, ques, pa1, pa2, pa3, pa4, correct):

self._q = ques

self._pa1 = pa1

self._pa2 = pa2

self._pa3 = pa3

self._pa4 = pa4

self._correct = correct

def get_ques(self):

return self._q

def get_pa1(self):

return self._pa1

def get_pa2(self):

return self._pa2

def get_pa3(self):

return self._pa3

def get_pa4(self):

return self._pa4

def get_correct(self):

return self._correct

def main():

lst_ques = [

Question("What is our nation's capital", 3, ["Texas", "Virginia", "Washington, D.C.", "New York"]),

Question("How many burrows make up New York city", 4, ["two", "four", "three", "five"]),

Question("In what month does the leap year occur", 1, ["February", "July", "December", "October"]),

Question("What state do the Cowboys football team play for", 2, ["New York", "Texas", "California", "Utah"]),

Question("What's the symbol to Iron", 1 ,["Fe", "Ie", "Ir", "In"]),

Question("Where is Notre Dame", 4 ,["Michigan", "Japan", "Ireland", "France"]),

Question("About how many billion years old is the sun", 3 ,["1", "4", "5", "2"]),

Question("What's the most malleable metal", 2 ,["iron", "gold", "aluminum", "steel"]),

Question("What is short for binary digit", 2 ,["bd", "bit", "bin", "digit"]),

Question("What is the Indiana state bird", 4 ,["robin", "eagle", "finch", "cardinal"]),

]

print('Player #1, please begin.')

i = 1

ca1 = 1

ques_attempted = []

while i<=5:

number = random.radiant(0,9)

if number not in ques_attempted:

print('Question',i)

print(lst_ques[number].get_ques())

print(lst_ques[number].get_pa1())

print(lst_ques[number].get_pa2())

print(lst_ques[number].get_pa3())

print(lst_ques[number].get_pa4())

answer = input('Enter in correct answer: ')

if ques[number].get_correct()==int(ans):

print('Correct!')

ca1+=1

else:

print('Incorrect')

ques_attempted.append(number)

i+= 1

print('Player #2, it is now your turn, please begin.')

i = 1

ca2 = 1

ques_attempted = []

while i<=5:

number = random.radiant(0,9)

if number not in ques_attempted:

print('Question',i)

print(lst_ques[number].get_ques())

print(lst_ques[number].get_pa1())

print(lst_ques[number].get_pa2())

print(lst_ques[number].get_pa3())

print(lst_ques[number].get_pa4())

answer = input('Enter in correct answer: ')

if ques[number].get_correct()==int(ans):

print("Correct!")

i = 1

ca2+=1

else:

print("Incorrect")

ques_attempted.append(number)

print('The final scores are: ')

print('Player #1: ', ca1)

print('Player #2: ', ca2)

if ca1 > ca2:

print('Player #1 is the winner.')

elif ca2 > ca1:

print('Player #2 is the winner.')

else:

print('The final scores are the same, the game is a tie.')

main()

它引发以下异常:

^{pr2}$

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号