Python 完全停留在我';我的课堂教学方法出了问题
我需要根据两名球员正确回答的问题数量来计算分数 我试图在类中插入ask()函数,但我不知道我做错了什么 我认为根据答案在问题后设置变量,然后将其发送到游戏会起作用,但显然我错了Python 完全停留在我';我的课堂教学方法出了问题,python,python-3.x,class,Python,Python 3.x,Class,我需要根据两名球员正确回答的问题数量来计算分数 我试图在类中插入ask()函数,但我不知道我做错了什么 我认为根据答案在问题后设置变量,然后将其发送到游戏会起作用,但显然我错了 import random PLAYER_1_SCORE = 0 PLAYER_2_SCORE = 0 TURN = 0 class Question(): def __init__(self, question, answer, options, score):
import random
PLAYER_1_SCORE = 0
PLAYER_2_SCORE = 0
TURN = 0
class Question():
def __init__(self, question, answer, options, score):
self.question = question
self.answer = answer
self.options = options
self.score = score
def ask(self,):
print (self.question + '?')
for n, option in enumerate(self.options):
print ('%d) %s' % (n + 1, option))
choice = int(input()) # answers are integers
if choice == self.answer:
score = 1
self.score = score
else:
score = 0
self.score = score
def get_score(self):
return score
questions = [
#1
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
#1
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
#3
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
#4
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
#5
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
#6
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
#7
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
#8
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
#9
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
#10
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
]
random.shuffle(questions)
def player_1():
for question in questions:
question.ask()
PLAYER_1_SCORE += question.get_score
def player_2():
for question in questions:
question.ask()
score = Question.get_score()
PLAYER_2_SCORE += score
def game_round():
while TURN < 10:
player_1()
player_2()
TURN += 1
def main():
play_again = 'Y'
if play_again == 'Y':
game_round
if PLAYER_1_SCORE > PLAYER_2_SCORE:
print ('Player 1 wins!')
elif PLAYER_1_SCORE == PLAYER_2_SCORE:
print ('Draw!')
else:
print ('Player 2')
play_again = input('Play again?')
else:
print('Goodbye!')
main()
随机导入
球员1分=0分
球员2分=0分
转动=0
课堂提问():
定义初始(自我、问题、答案、选项、分数):
self.question=问题
自我回答
self.options=选项
self.score=分数
def ask(self,):
打印(self.question+“?”)
对于n,枚举中的选项(self.options):
打印(“%d)%s%”(n+1,选项))
choice=int(input())#答案是整数
如果选择==自我回答:
分数=1
self.score=分数
其他:
分数=0
self.score=分数
def get_分数(自我):
回击得分
问题=[
#1
问题(问题=“一匹马有几条腿”,
答案=4,
选项=['one',
“两个”,
"三",,
"四",,
"五"),,
#1
问题(问题=“一匹马有几条腿”,
答案=4,
选项=['one',
“两个”,
"三",,
"四",,
"五"),,
#3
问题(问题=“一匹马有几条腿”,
答案=4,
选项=['one',
“两个”,
"三",,
"四",,
"五"),,
#4
问题(问题=“一匹马有几条腿”,
答案=4,
选项=['one',
“两个”,
"三",,
"四",,
"五"),,
#5
问题(问题=“一匹马有几条腿”,
答案=4,
选项=['one',
“两个”,
"三",,
"四",,
"五"),,
#6
问题(问题=“一匹马有几条腿”,
答案=4,
选项=['one',
“两个”,
"三",,
"四",,
"五"),,
#7
问题(问题=“一匹马有几条腿”,
答案=4,
选项=['one',
“两个”,
"三",,
"四",,
"五"),,
#8
问题(问题=“一匹马有几条腿”,
答案=4,
选项=['one',
“两个”,
"三",,
"四",,
"五"),,
#9
问题(问题=“一匹马有几条腿”,
答案=4,
选项=['one',
“两个”,
"三",,
"四",,
"五"),,
#10
问题(问题=“一匹马有几条腿”,
答案=4,
选项=['one',
“两个”,
"三",,
"四",,
"五"),,
]
随机。随机(问题)
def播放器_1():
有关问题:
问
玩家1分+=问题。获得分数
def播放器_2():
有关问题:
问
分数=问题。获得分数()
球员2分+=得分
def game_round():
转弯<10时:
玩家_1()
玩家_2()
转弯+=1
def main():
再次播放_='Y'
如果再次播放_==“Y”:
游戏回合
如果玩家1分>玩家2分:
打印('玩家1获胜!')
elif球员1分=球员2分:
打印('Draw!')
其他:
打印('Player 2')
再次播放=输入(“再次播放”)
其他:
打印('再见!')
main()
存在不同的问题:
self。
在get\u score(self)
方法中随机导入
课堂提问():
定义初始(自我、问题、对、错):
self.question=问题
self.right=right
self.options=[右]+列表(错误)
def ask(自我):
随机.shuffle(self.options)
对于n,枚举中的选项(self.options):
打印(“{}{}”。格式(n+1,选项))
选择=int(输入(self.question+“?”)-1
返回self.options[选择]==self.right
职业球员:
定义初始化(self,name):
self.name=名称
self.score=0
定义报告(自我):
返回“”。格式(self.name、self.score)
班级游戏:
定义初始(自我、玩家、问题):
self.players=玩家
self.questions=问题
def重置_分数(自我):
对于self.players中的玩家:
player.score=0
def启动(自):
随机。洗牌(自我。玩家)
对于self.players中的玩家:
打印(“#”*(len(player.name)+12))
打印(“#{}的回合:#”。格式(player.name))
打印(“#”*(len(player.name)+12))
打印()
随机。洗牌(自我提问)
对于我,列举问题(自我问题):
打印(“问题{}:”.format(i+1))
如果有问题,请提问()
player.score+=1
import random
PLAYER_1_SCORE = 0
PLAYER_2_SCORE = 0
TURN = 0
class Question():
def __init__(self, question, answer, options):
self.question = question
self.answer = answer
self.options = options
self.score = 0
def ask(self,):
print (self.question + '?')
for n, option in enumerate(self.options):
print ('%d) %s' % (n + 1, option))
choice = int(input()) # answers are integers
if choice == self.answer:
self.score = 1
else:
self.score = 0
def get_score(self):
return self.score
questions = [
#1
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
#1
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
#3
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
#4
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
#5
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
#6
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
#7
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
#8
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
#9
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
#10
Question(question='How many legs on a horse',
answer=4,
options=['one',
'two',
'three',
'four',
'five']),
]
random.shuffle(questions)
def player_1():
for question in questions:
question.ask()
global PLAYER_1_SCORE
PLAYER_1_SCORE += question.get_score()
def player_2():
for question in questions:
question.ask()
global PLAYER_2_SCORE
PLAYER_2_SCORE += question.get_score()
def game_round():
player_1()
player_2()
global TURN
TURN += 1
def main():
play_again = 'Y'
while play_again == 'Y':
game_round()
if PLAYER_1_SCORE > PLAYER_2_SCORE:
print ('Player 1 wins!')
elif PLAYER_1_SCORE == PLAYER_2_SCORE:
print ('Draw!')
else:
print ('Player 2 wins!')
play_again = input('Play again?')
else:
print('Goodbye!')
main()
import random
class Question():
def __init__(self, question, right, wrong):
self.question = question
self.right = right
self.options = [right] + list(wrong)
def ask(self):
random.shuffle(self.options)
for n, option in enumerate(self.options):
print ("{}) {}".format(n + 1, option))
choice = int(input(self.question + "? ")) - 1
return self.options[choice] == self.right
class Player:
def __init__(self, name):
self.name = name
self.score = 0
def __repr__(self):
return "<Player {} score:{}>".format(self.name, self.score)
class Game:
def __init__(self, players, questions):
self.players = players
self.questions = questions
def reset_score(self):
for player in self.players:
player.score = 0
def start(self):
random.shuffle(self.players)
for player in self.players:
print("#"*(len(player.name)+12))
print("# {}´s turn: #".format(player.name))
print("#"*(len(player.name)+12))
print()
random.shuffle(self.questions)
for i, question in enumerate(self.questions):
print("Question #{}:".format(i+1))
if question.ask():
player.score += 1
print("-"*40)
def check_winner(self):
players = sorted(self.players, key=lambda x: x.score, reverse=True)
top_score = players[0].score
if players[1].score == top_score:
print("Draw! {} got the same score.".format(", ".join([player.name for player in players if player.score == top_score])))
else:
print("Player {} wins!".format(players[0].name))
questions = [
Question( #1
question='How many legs on a horse',
right='Four',
wrong=(
'One',
'Two',
'Three',
'Five',
)
),
Question( #2
question='How many legs on a cow',
right='Four',
wrong=(
'One',
'Two',
'Three',
'Five',
)
),
Question( #3
question='How many legs on a spider',
right='Eight',
wrong=(
'Six',
'Ten',
'Four',
)
),
]
def main():
n_players = int(input("How many players are going to play? "))
if n_players < 2:
raise ValueError("Minimum of 2 players required")
players = []
for i in range(n_players):
players.append(Player(input("How is player #{} called? ".format(i+1))))
print()
game = Game(players, questions)
play_again = True
while play_again:
game.reset_score()
game.start()
game.check_winner()
play_again = input("Play again? ").lower() == 'y'
else:
print("Goodbye!")
if __name__ == '__main__':
main()