我是一名初学者程序员,并且在家从书本中自学python编程。我目前正在学习字符串。我在那里解决了一个问题,但我在想是否还有其他更简单的方法可以解决它。
问:某位教授给出100分的考试,其等级分为90-100:A,80-89:B,70-79:C,60-69:D和<60:F。编写一个接受考试成绩作为输入并打印出相应成绩的程序。def main():
## making a list from 0-100
num = list(range(0,101))
## asking for the exam points
points = int(input("Enter the exam points 0-100: "))
## iterating num 0-60
for i in num[0:60]:
if i == points:
print("Your corresponding grade is F.")
## iterating num 60-70
for i in num[60:70]:
if i == points:
print("Your corresponding grade is D.")
## iterating num 70-80
for i in num[70:80]:
if i == points:
print("Your corresponding grade is C.")
## iterating num 80-90
for i in num[80:90]:
if i == points:
print("Your corresponding grade is B.")
## iterating num 90-100
for i in num[90:101]:
if i == points:
print("Your corresponding grade is A.")
main()
答案 0 :(得分:4)
是的,有一种更好的书写方式。
鉴于您拥有一个整数,即分数,您所要做的就是将其与确定成绩的边界进行比较(使用<
或>
)。
points = int(input("Enter the exam points 0-100: "))
if points < 60:
grade = 'F'
elif points < 70:
grade = 'D'
elif points < 80:
grade = 'C'
elif points < 90:
grade = 'B'
else:
grade = 'A'
print("Your corresponding grade is", grade)
为使代码更清晰,您可以将比较逻辑放入返回给定分数的分数的函数中。
def calculate_grade(score):
if score < 60:
return 'F'
if score < 70:
return 'D'
if score < 80:
return 'C'
if score < 90:
return 'B'
return 'A'
def main():
points = int(input("Enter the exam points 0-100: "))
grade = calculate_grade(points)
print("Your corresponding grade is", grade)
答案 1 :(得分:3)
还有一种更简单,更简洁的方法来执行此操作。试试这个:
points = int(input("Enter the exam points 0-100: "))
if 0 < points <= 60:
print "Your grade is F"
elif 60 < points <= 70:
print "Your grade is E"
elif 70 < points <= 80:
print "Your grade is D"
[and so on...]
好处是:
if
时退出答案 2 :(得分:1)
这是正确的(给出了预期的分数)并且效率很低:
此外,您使用纯线性(非结构化)设计。
您可以:
代码示例
def points_to_grade(points):
limits = [90, 80, 70, 60]
grades = ['A', 'B', 'C', 'D']
for i,limit in enumerate(limits):
if points >= limit:
return grade[i]
return 'F'
def main():
## asking for the exam points
points = int(input("Enter the exam points 0-100: "))
## convert to grade
grade = points_to_grade(points)
print("Your corresponding grade is {}.".format(grade))
答案 3 :(得分:1)
<?php
namespace App\Tests\Service;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class UserPropertyServiceTest extends KernelTestCase
{
/** @var UserPropertyService */
private $myService;
public function setUp() {
self::bootKernel();
$this->myService = self::$kernel->getContainer()->get('app.user_management.user_property_service');
}
}
答案 4 :(得分:0)
我不使用python编写代码,因此我可能不会使用正确的语法,但是在所有语言中这几乎都是相同的。遍历所有值是不好的做法。您只需要很少的if语句(或switch / case),就像这样:
if i<=100 and i>=90:
print("Grade is A")
if i<=89 and i>=80:
print("Grade is B")
等...
答案 5 :(得分:0)
def main():
points = int(input('Enter the exam points:'))
if points >= 90:
print('Your corresponding grade is A')
elif 80 <= points <= 90:
print('Your corresponding grade is B.')
elif 70 <= points <= 80:
print('Your corresponding grade is C.')
elif 60 <= points <= 70:
print('Your corresponding grade is D.')
elif 0 <= points <= 60:
print('Your corresponding grade is F.')
答案 6 :(得分:-1)
您的解决方案并不是最佳选择,可以使用一些基本逻辑(如下所示)很好地解决,该逻辑将表现更好并且更具可读性:
## asking for the exam points
point = int(input("Enter the exam points 0-100: "))
point_set = 'FEDCBA'
if point == 100:
print('A')
else:
print(point_set[point // 10 - 5])
代码使用简单的逻辑,一旦点小于60,则// // 10的结果为5和5-5 = 0,因此将使用索引0的等级-F。如果点为100,则为边缘情况,这就是为什么我使用特殊情况,如果其他情况下有简单的数学,这是不言自明的。
答案 7 :(得分:-3)
是的,我会使用这种方法
def main():
points = int(input("Enter the exam points 0-100: "))
if points >= 60:
print("Your corresponding grade is F.")
elif points > 60 and points < 70:
print("Your corresponding grade is D.")
elif points > 70 and points < 80:
print("Your corresponding grade is C.")
elif points > 80 and points < 90:
print("Your corresponding grade is B.")
elif points > 90:
print("Your corresponding grade is A.")
else:
print("Error: invalid value")