Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有一种更简单的方法可以打印给定的变量而不必反复使用print函数?_Python - Fatal编程技术网

Python 有没有一种更简单的方法可以打印给定的变量而不必反复使用print函数?

Python 有没有一种更简单的方法可以打印给定的变量而不必反复使用print函数?,python,Python,我肯定有,我是一个初学者,在这里奋斗了很长时间!我知道它可以用更简单的打印语句来完成,但它似乎不适用于以下情况: def calc_grade(n): if n < 40: return "E" elif n < 50: return "D" elif n < 60: return "C" elif n < 70: r

我肯定有,我是一个初学者,在这里奋斗了很长时间!我知道它可以用更简单的打印语句来完成,但它似乎不适用于以下情况:

def calc_grade(n):
    if n < 40:
        return "E"
    elif n < 50:
        return "D"
    elif n < 60:
        return "C"
    elif n < 70:
        return "B"
    else:
        return "A"

n = 35
print("The grade letter for the mark 35 is " + calc_grade(n))

n = 4
print("The grade letter for the mark 44 is " + calc_grade(n))

n = 52
print("The grade letter for the mark 52 is " + calc_grade(n))

n = 65
print("The grade letter for the mark 65 is " + calc_grade(n))

n = 88
print("The grade letter for the mark 88 is " + calc_grade(n))

是的,使用for循环非常简单:

points = [35, 44, 52, 65, 88]
for n in points: 
    print(f"The grade letter for the mark {n} is {calc_grade(n)}")

是的,使用for循环非常简单:

points = [35, 44, 52, 65, 88]
for n in points: 
    print(f"The grade letter for the mark {n} is {calc_grade(n)}")

您可以使用一行代码打印所有值:

vals = [35, 44, 52, 65, 88]
print(*(f'The grade letter for the mark {v} is {calc_grade(v)}' for v in vals), sep='\n')
结果:

The grade letter for the mark 35 is E
The grade letter for the mark 44 is D
The grade letter for the mark 52 is C
The grade letter for the mark 65 is B
The grade letter for the mark 88 is A
[EXTRA]这里有一个改进calc_grade函数的可能解决方案:

import math

grades = [(40, 'E'), (50, 'D'), (60, 'C'), (70, 'B'), (math.inf, 'A')]
def calc_grade(n):
    return next(g for v, g in grades if n < v)

您可以使用一行代码打印所有值:

vals = [35, 44, 52, 65, 88]
print(*(f'The grade letter for the mark {v} is {calc_grade(v)}' for v in vals), sep='\n')
结果:

The grade letter for the mark 35 is E
The grade letter for the mark 44 is D
The grade letter for the mark 52 is C
The grade letter for the mark 65 is B
The grade letter for the mark 88 is A
[EXTRA]这里有一个改进calc_grade函数的可能解决方案:

import math

grades = [(40, 'E'), (50, 'D'), (60, 'C'), (70, 'B'), (math.inf, 'A')]
def calc_grade(n):
    return next(g for v, g in grades if n < v)

您的代码应该可以正常工作。您收到了什么错误消息

无论如何:要简化代码,可以使用for循环:

points = [35, 44, 52, 65, 88]
for n in points: 
    print(f"The grade letter for the mark {n} is {calc_grade(n)}")

您的代码应该可以正常工作。您收到了什么错误消息

无论如何:要简化代码,可以使用for循环:

points = [35, 44, 52, 65, 88]
for n in points: 
    print(f"The grade letter for the mark {n} is {calc_grade(n)}")

可以使用所有可能的值定义数组 然后通过一个for-each循环遍历每个值

array = [35, 44, 52, 65, 88]
for n in array:
    print(f"The grade letter for the mark {n} is {calc_grade(n)}")
输出
可以使用所有可能的值定义数组 然后通过一个for-each循环遍历每个值

array = [35, 44, 52, 65, 88]
for n in array:
    print(f"The grade letter for the mark {n} is {calc_grade(n)}")
输出
创建用于打印的函数

def calc_grade(n):
    if n < 40:
        return "E"
    elif n < 50:
        return "D"
    elif n < 60:
        return "C"
    elif n < 70:
        return "B"
    else:
        return "A"

def print_function(n):
    print("The grade letter for the mark " + str(n) + " is " + calc_grade(n))

print_function(35)
print_function(44)
print_function(52)
print_function(65)

创建用于打印的函数

def calc_grade(n):
    if n < 40:
        return "E"
    elif n < 50:
        return "D"
    elif n < 60:
        return "C"
    elif n < 70:
        return "B"
    else:
        return "A"

def print_function(n):
    print("The grade letter for the mark " + str(n) + " is " + calc_grade(n))

print_function(35)
print_function(44)
print_function(52)
print_function(65)



我不清楚什么不起作用。使用循环?。。。n的值从何而来?您可以将print函数移动到方法中,将求值后的值赋给变量并打印。这里有5条print语句。我想有一种方法可以只使用一个打印语句,每次更改n的值。不需要感谢您的评论。。。看,我不清楚什么不起作用。使用循环?。。。n的值从何而来?您可以将print函数移动到方法中,将求值后的值赋给变量并打印。这里有5条print语句。我想有一种方法可以只使用一个打印语句,每次更改n的值。不需要感谢您的评论。。。确实看到了不错的解决方案,但也许使用f字符串会更干净->确实不错的解决方案,但也许使用f字符串会更干净->虽然这是对工作部分的改进,但这并不是对问题的答案,如果他们得到无限等级怎么办;说真的,虽然我认为我更喜欢把A从列表中去掉,如果n.8.5重新检查语法。复制粘贴我的代码。注意错误的压痕。