question3

用Python实现斐波那契数列,费波那契数列由0和1开始,之后的费波那契系数就是由之前的两数相加而得出,例如前 斐波那契数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34。用数学方法可定义为如图所示:
这里写图片描述

# -*- coding : UTF-8 -*-

nums = int(input("你需要几项(1~50)?"))

# 第一和第二项
Fibo_1 = 0
Fibo_2 = 1
count = 2

# 判断输入的值是否合法
if nums <= 0 or nums >= 51 :
   print("请输入一个不大于50的正整数。")
elif nums == 1:
   print("斐波那契数列:")
   print(Fibo_1)
else:
   print("斐波那契数列:")
   print(Fibo_1,",",Fibo_2,end=" , ")
   while count < nums:
       Fibo = Fibo_1 + Fibo_2
       print(Fibo,end=" , ")
       # 更新值
       Fibo_1 = Fibo_2
       Fibo_2 = Fibo
       count += 1
class Question: def __init__(self, stem, options, answer): self.stem = stem self.options = options self.answer = answerclass QuestionBank: def __init__(self): self.questions = [] def add_question(self, question): self.questions.append(question) def remove_question(self, question): self.questions.remove(question) def get_random_questions(self, num): return random.sample(self.questions, num)class Paper: def __init__(self, questions): self.questions = questions self.answers = {} def answer_question(self, question, answer): self.answers[question] = answer def get_score(self): score = 0 for question, answer in self.answers.items(): if answer == question.answer: score += 1 return scoreclass Grader: def __init__(self, paper): self.paper = paper def grade(self): return self.paper.get_score()# Example usagequestion1 = Question("What is the capital of France?", ["Paris", "London", "Berlin", "Madrid"], "Paris")question2 = Question("What is the largest planet in the solar system?", ["Mercury", "Venus", "Earth", "Jupiter"], "Jupiter")question3 = Question("What is the highest mountain in the world?", ["K2", "Mount Everest", "Makalu", "Cho Oyu"], "Mount Everest")question_bank = QuestionBank()question_bank.add_question(question1)question_bank.add_question(question2)question_bank.add_question(question3)paper = Paper(question_bank.get_random_questions(2))paper.answer_question(question1, "Paris")paper.answer_question(question2, "Jupiter")grader = Grader(paper)score = grader.grade()print("Your score is:", score)将这个代码转为C++的
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值