如何在 Python 中创建表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33458074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:23:14  来源:igfitidea点击:

How To Create A Table in Python

pythonfunctionpython-3.xtabstop

提问by Baroness Sledjoys

I've posted this before, but I'm reposting it with more detailed information.

我以前发布过这个,但我重新发布了更详细的信息。

This is my assignment: enter image description here

这是我的任务: 在此处输入图片说明

enter image description here

在此处输入图片说明

And, so far, this is my code:

而且,到目前为止,这是我的代码:

# Define a function that caculates average test scores
def calculate_average(score1, score2, score3, score4, score5):
    total = score1 + score2 + score3 + score4 + score5
    average = total % 5
    print(average)

# Define a function that determines a letter grade
# Based on the given score
def determine_grade(score):
    if score < 60:
        print 'F'
    elif score => 60 or score <= 69:
        print 'D'
    elif score => 70 or score <= 79:
        print 'C'
    elif score => 80 or score <= 89:
        print 'B'
    elif score => 90 or score <= 100:
        print 'A'
# Define a function that prompts the user to input names and test scores
def input_data():
    score1 = input('Enter score 1:')
    name1 = input('Enter name 1:')
    score2 = input('Enter score 2:')
    name2 = input('Enter name 2:')
    score3 = input('Enter score 3:')
    name3 = input('Enter name 3:')
    score4 = input('Enter score 4:')
    name4 = input('Enter name 4:')
    score5 = input('Enter score 5:')
    name5 = input('Enter name 5:')


# Define a function that displays a letter grade for each score
# followed by the student's name and the average test score
def display_menu():

As I said in the first post, I don't have a real problem with anything except setting up that table. I'm really confused!

正如我在第一篇文章中所说,除了设置那张桌子之外,我对任何事情都没有真正的问题。我真的很困惑!

I asked my instructor (this is an online course, BTW), and he said: "Its just using the print operator, like you have done in previous modules, your printing text , variables and the return value of a function."

我问过我的导师(这是一个在线课程,顺便说一句),他说:“它只是使用打印运算符,就像你在之前的模块中所做的那样,打印文本、变量和函数的返回值。”

Now I'm just wondering where to start.

现在我只是想知道从哪里开始。

EDIT:I've updated my code this:

编辑:我已经更新了我的代码:

# Define a function that prompts the user to input names and test scores
def input_data():
    score1 = input('Enter score 1:')
    name1 = input('Enter name 1:')
    score2 = input('Enter score 2:')
    name2 = input('Enter name 2:')
    score3 = input('Enter score 3:')
    name3 = input('Enter name 3:')
    score4 = input('Enter score 4:')
    name4 = input('Enter name 4:')
    score5 = input('Enter score 5:')
    name5 = input('Enter name 5:')

# Define a function that determines a letter grade
# Based on the given score
def determine_grade(score):
    if score < 60:
        print ('F')
    elif 60 <= score <= 69:
        print ('D')
    elif  70 <= score <= 79:
        print ('C')
    elif 80 <= score <= 89:
        print ('B')
    elif 90 <= score <= 100:
        print ('A')

# Define a function that caculates average test scores
def calculate_average(score1, score2, score3, score4, score5):
    total = score1 + score2 + score3 + score4 + score5
    average = total / 5
    print(average)

# Define a function that displays a letter grade for each score
# followed by the student's name and the average test score
def display_menu():
    for x in range(10):
        print("{:<10}".format("{:0.1f}".format(x)), end='')
    print ("Name\t\t\tnumeric grade\t\tletter grade")
print ("---------------------------------------------------------------")
print ("%s:\t\t\t%f\t\t%s") % ('name1', 'score1', determine_grade)
print ("%s:\t\t\t%f\t\t%s") % ('name2', 'score2', determine_grade)
print ("%s:\t\t\t%f\t\t%s") % ('name3', 'score3', determine_grade)
print ("%s:\t\t\t%f\t\t%s") % ('name4', 'score4', determine_grade)
print ("%s:\t\t\t%f\t\t%s") % ('name5', 'score5', determine_grade)
print ("---------------------------------------------------------------")

But when I run it: enter image description here

但是当我运行它时: 在此处输入图片说明

EDIT #2:This is my code currently:

编辑#2:这是我目前的代码:

# Define a function that prompts the user to input names and test scores
def input_data():
    score1 = input('Enter score 1:')
    name1 = input('Enter name 1:')
    score2 = input('Enter score 2:')
    name2 = input('Enter name 2:')
    score3 = input('Enter score 3:')
    name3 = input('Enter name 3:')
    score4 = input('Enter score 4:')
    name4 = input('Enter name 4:')
    score5 = input('Enter score 5:')
    name5 = input('Enter name 5:')

# Define a function that caculates average test scores
def calculate_average(score1, score2, score3, score4, score5):
    total = score1 + score2 + score3 + score4 + score5
    average = total / 5
    print(average)

# Define a function that determines a letter grade
# Based on the given score
def determine_grade(score):
    if score < 60:
        print ('F')
    elif 60 <= score <= 69:
        print ('D')
    elif  70 <= score <= 79:
        print ('C')
    elif 80 <= score <= 89:
        print ('B')
    elif 90 <= score <= 100:
        print ('A')



# Define a function that displays a letter grade for each score
# followed by the student's name and the average test score
def display_menu():
   print ("Name\t\t\tnumeric grade\t\tlettergrade")
print ("---------------------------------------------------------------")
print ("%s:\t\t\t%f\t\t%s" % ('name1', 93, 'A'))
print ("%s:\t\t\t%f\t\t%s" % ('name2', 89, 'B'))
print ("%s:\t\t\t%f\t\t%s" % ('name3', 76, 'C'))
print ("%s:\t\t\t%f\t\t%s" % ('name4', 58, 'F'))
print ("%s:\t\t\t%f\t\t%s" % ('name5', 98, 'A'))
print ("---------------------------------------------------------------")
print (calculate_average)

And this is what happens when I run it:

这就是我运行它时发生的情况:

enter image description here

在此处输入图片说明

Now, I have mainly two problems:

现在,我主要有两个问题:

1) How do I get the input statements to execute and enter the data BEFORE the table is displayed?

1) 如何在显示表之前获取要执行的输入语句并输入数据?

2) How do I convert the numbers displayed so that they are in the '.2f' format? (I've already tried a few ways and none of them worked).

2) 如何将显示的数字转换为“.2f”格式?(我已经尝试了几种方法,但都没有奏效)。

HOPEFULLY THE FINAL EDIT:I'm getting really close to the solution, but need help with a few more things.

希望最终编辑:我非常接近解决方案,但还需要一些帮助。

Here is my code:

这是我的代码:

# Define a function that prompts the user to input names and test scores

score = input('Enter score 1:')
name1 = input('Enter name 1:')
score = input('Enter score 2:')
name2 = input('Enter name 2:')
score = input('Enter score 3:')
name3 = input('Enter name 3:')
score = input('Enter score 4:')
name4 = input('Enter name 4:')
score = input('Enter score 5:')
name5 = input('Enter name 5:')




# Define a function that determines a letter grade
# Based on the given score
def determine_grade(score):
    if score < 60:
        print ('F')
    elif 60 <= score <= 69:
        print ('D')
    elif  70 <= score <= 79:
        print ('C')
    elif 80 <= score <= 89:
        print ('B')
    elif 90 <= score <= 100:
        print ('A')

determine_grade(score)


# Define a function that caculates average test scores
def calculate_average(score):
    total = score + score + score + score + score
    average = total / 5
    print(average)

calculate_average(score)

# Define a function that displays a letter grade for each score
# followed by the student's name and the average test score
def display_menu():
   print ("Name\t\t\tnumeric grade\t\tlettergrade")
print ("---------------------------------------------------------------")
print ("%s:\t\t\t%f\t\t%s" % ('name1', 'score', determine_grade('score')))
print ("%s:\t\t\t%f\t\t%s" % ('name2', 'score', determine_grade('score')))
print ("%s:\t\t\t%f\t\t%s" % ('name3', 'score', determine_grade('score')))
print ("%s:\t\t\t%f\t\t%s" % ('name4', 'score', determine_grade('score')))
print ("%s:\t\t\t%f\t\t%s" % ('name5', 'score', determine_grade('score')))
print ("---------------------------------------------------------------")
calculate_average(score)

And this is what happens when I press F5:

这就是我按 F5 时发生的情况:

enter image description here

在此处输入图片说明

GUYS, I'M ALMOST DONE JUST NEED HELP WITH FORMATTING:I created another file so I could reorganize my code a bit, so that's why there's no comments. This is what I have:

伙计们,我快完成了,只是需要格式化方面的帮助:我创建了另一个文件,这样我就可以稍微重新组织一下我的代码,这就是为什么没有评论的原因。这就是我所拥有的:

score1 = float(input('Enter score 1:'))
name1 = input('Enter name 1:')
score2 = float(input('Enter score 2:'))
name2 = input('Enter name 2:')
score3 = float(input('Enter score 3:'))
name3 = input('Enter name 3:')
score4 = float(input('Enter score 4:'))
name4 = input('Enter name 4:')
score5 = float(input('Enter score 5:'))
name5 = input('Enter name 5:')


def determine_letter_grade1(score1):
    if score1 < 60.0:
        print ('F')
    elif 60.0 <= score1 <= 69.0:
        print ('D')
    elif  70.0 <= score1 <= 79.0:
        print ('C')
    elif 80.0 <= score1 <= 89.0:
        print ('B')
    elif 90.0 <= score1 <= 100.0:
        print ('A')

def determine_letter_grade2(score2):
    if score2 < 60.0:
        print ('F')
    elif 60.0 <= score2 <= 69.0:
        print ('D')
    elif  70.0 <= score2 <= 79.0:
        print ('C')
    elif 80.0 <= score2 <= 89.0:
        print ('B')
    elif 90.0 <= score2 <= 100.0:
        print ('A')

def determine_letter_grade3(score3):
    if score3 < 60.0:
        print ('F')
    elif 60.0 <= score3 <= 69.0:
        print ('D')
    elif  70.0 <= score3 <= 79.0:
        print ('C')
    elif 80.0 <= score3 <= 89.0:
        print ('B')
    elif 90.0 <= score3 <= 100.0:
        print ('A')

def determine_letter_grade4(score4):
    if score4 < 60.0:
        print ('F')
    elif 60.0 <= score4 <= 69.0:
        print ('D')
    elif  70.0 <= score4 <= 79.0:
        print ('C')
    elif 80.0 <= score4 <= 89.0:
        print ('B')
    elif 90.0 <= score4 <= 100.0:
        print ('A')

def determine_letter_grade5(score5):
    if score5 < 60.0:
        print ('F')
    elif 60.0 <= score5 <= 69.0:
        print ('D')
    elif  70.0 <= score5 <= 79.0:
        print ('C')
    elif 80.0 <= score5 <= 89.0:
        print ('B')
    elif 90.0 <= score5 <= 100.0:
        print ('A')

average = (score1 + score2 + score3 + score4 + score5) / 5.0

def determine_letter_grade_avg(average):
    if average < 60.0:
        print ('F')
    elif 60.0 <= average <= 69.0:
        print ('D')
    elif  70.0 <= average <= 79.0:
        print ('C')
    elif 80.0 <= average <= 89.0:
        print ('B')
    elif 90.0 <= average <= 100.0:
        print ('A')

def display_menu():
    for x in range(10):
        print("{:<10}".format("{:0.1f}".format(x)), end='')
    print ("Name\t\t\tnumeric grade\t\tletter grade")
print ("---------------------------------------------------------------")
print ("%s:\t\t\t%f\t\t%s" % (name1, score1, determine_letter_grade1(score1)))
print ("%s:\t\t\t%f\t\t%s" % (name2, score2, determine_letter_grade2(score2)))
print ("%s:\t\t\t%f\t\t%s" % (name3, score3, determine_letter_grade3(score3)))
print ("%s:\t\t\t%f\t\t%s" % (name4, score4, determine_letter_grade4(score4)))
print ("%s:\t\t\t%f\t\t%s" % (name5, score5, determine_letter_grade5(score5)))
print ("---------------------------------------------------------------")
print ('Average Score:', average, determine_letter_grade_avg(average))

And when I run it:

当我运行它时:

enter image description here

在此处输入图片说明

回答by Steven Summers

You should be looking towards str.format()and end=''for your print statement

您应该寻找str.format()end=''寻找您的印刷声明

Here's a little example

这是一个小例子

for x in range(10):
    print("{:<10}".format("{:0.1f}".format(x)), end='')

The first part for the rounding to 1 decimal place for your float values is

将浮点值舍入到小数点后一位的第一部分是

"{:0.1f}".format(x)

The 0is for no left padding, the following .1fis for rounding to 1 decimal place. Next is the main part

0是对于没有左填充,以下.1f是用于四舍五入至1个小数位。接下来是主要部分

"{:<10}".format(...)

This will print with a minimum string length of 10 characters. So if you have a string such as Helloit is printed as Hello_____( _ to represent empty spaces) The 10can be changed to a value of your choosing.

这将打印最少 10 个字符的字符串长度。因此,如果您有一个字符串,例如Hello它被打印为Hello_____( _ 表示空格),则10可以将其更改为您选择的值。

Finally the end=''. By default the endparameter is \nwhich is what creates the new line after each print, but by changing it you can form lines from multiple print statements. Just print normally or set end='\n'when you want to end the line.

最后将end=''. 默认情况下,end参数是\nwhich 在每次打印后创建新行,但通过更改它,您可以从多个打印语句中形成行。只需正常打印或end='\n'在要结束该行时进行设置。

Little something to note as well. When checking if a value is between two integers you can use this instead of checking it twice (also you would need and andoperator instead of or)

还有一点要注意。当检查一个值是否在两个整数之间时,您可以使用 this 而不是检查两次(您也需要 andand运算符而不是or

elif 60 <= score <= 69:

回答by Arun Das

You could do something like this:

你可以这样做:

def display_menu():
print "Name\t\t\tnumeric grade\t\tlettergrade"
print "---------------------------------------------------------------"
print "%s:\t\t\t%f\t\t%s" % ('name1', 50, 'F')
print "%s:\t\t\t%f\t\t%s" % ('name2', 50, 'F')
print "%s:\t\t\t%f\t\t%s" % ('name3', 23, 'F')
print "%s:\t\t\t%f\t\t%s" % ('name4', 44, 'F')
print "%s:\t\t\t%f\t\t%s" % ('name5', 48, 'F')
print "---------------------------------------------------------------"

display_menu()

显示菜单()

I used \tto give tabs in between.

我曾经\t在两者之间给出标签。

This is the screenshot of the PythonShell Output

这是 PythonShell 输出的屏幕截图

回答by Baroness Sledjoys

Well, I just got the solution back, and I did a real butchering on the code. But, I will post this so no one will have to suffer like I did:

好吧,我刚刚得到了解决方案,并且我对代码进行了真正的屠杀。但是,我会发布这篇文章,所以没有人会像我一样受苦:

# main function
    def main():
        # Local variables only have to define floats
        average = 0.0
        score1 = 0.0
        score2 = 0.0
        score3 = 0.0
        score4 = 0.0
        score5 = 0.0

        # Get scores
        score1 = float(input('Enter score 1:'))
        name1  = input('Enter name 1:')
        score2 = float(input('Enter score 2:'))
        name2  = input('Enter name 2:')
        score3 = float(input('Enter score 3:'))
        name3  = input('Enter name 3:')
        score4 = float(input('Enter score 4:'))
        name4  = input('Enter name  4:')
        score5 = float(input('Enter score 5:'))
        name5  = input('Enter name 5:')

        # Calculate average grade
        average = calculate_average(score1, score2, score3, score4, score5)

        #Display grade and average information in tabular form
        print('Name\t\tnumeric grade\tletter grade')
        print('----------------------------------------------------')
        print(name1 + ':\t\t', score1, '\t\t', determine_grade(score1))
        print(name2 + ':\t\t', score2, '\t\t', determine_grade(score2))
        print(name3 + ':\t\t', score3, '\t\t', determine_grade(score3))
        print(name4 + ':\t\t', score4, '\t\t', determine_grade(score4))
        print(name5 + ':\t\t', score5, '\t\t', determine_grade(score5))
        print('----------------------------------------------------')
        print ('Average score:\t', average, '\t\t', \
               determine_grade(average))

    # The calc_average function returns average of 5 grades 
    def calculate_average(s1, s2, s3, s4, s5):
        return  (s1 + s2 + s3 + s4 + s5) / 5.0

    # The determine_grade function receives a numeric  
    # grade and returns the corresponding letter grade 
    def determine_grade(score):
        if score >= 90:
            return 'A'
        elif score >= 80:
            return 'B'
        elif score >= 70:
            return 'C'
        elif score >= 60:
            return 'D'
        else:
            return 'F'

    # Call the main function.
    main()