Python 使用已定义函数的脚本

Python 使用已定义函数的脚本,python,python-3.x,Python,Python 3.x,我正在编写一个脚本,要求用户提供两个数字作为一个时间表的最大值,然后使用我已经定义的两个函数绘制该表。然而,我不知道如何将所有的事情结合起来来完成这一点。以下是我所拥有的: print("What is the maximum value for the first factor?") number1 = input() print("What is the maximun value for the second factor?") number2 = input() print("Here

我正在编写一个脚本,要求用户提供两个数字作为一个时间表的最大值,然后使用我已经定义的两个函数绘制该表。然而,我不知道如何将所有的事情结合起来来完成这一点。以下是我所拥有的:

print("What is the maximum value for the first factor?")
number1 = input()
print("What is the maximun value for the second factor?")
number2 = input()
print("Here is your times table:")
def times_table(times,factor):
    """takes two positive integers and returns a list of lists

    int, int -> list of lists"""
    table = [[0]*(factor+1)]
    for i in range(1,times+1):
        table.append(list(range(0,i*factor+1,i)))
    return table  


def print_table(listx):
    """takes a list of lists of numbers and displays them on the screen

    list of numbers -> numbers"""
    for x in listx:
        for y in x:
            print('{0:<10}'.format(y), end='')
        print()
print(“第一个因子的最大值是多少?”)
number1=输入()
打印(“第二个因素的最大值是多少?”)
number2=输入()
打印(“这是您的时刻表:”)
def时间_表(时间,系数):
“”接受两个正整数并返回列表列表
int,int->列表列表“”
表=[[0]*(系数+1)]
对于范围内的i(1,乘以+1):
表.追加(列表(范围(0,i*因子+1,i)))
返回表
def打印表格(列表X):
“”“获取数字列表并将其显示在屏幕上
数字列表->数字“”
对于listx中的x:
对于x中的y:

print(“{0:假设
number1
number2
将作为
times\u table()
的参数提供,您只缺少两个函数调用:

# Calls times_table function providing number1 and number2 as arguments
# And assigns the returned variable to my_list
my_list = times_table(number1,number2)
# Call print_table function providing the list created in times_table function
print_table(my_list)

必须做两件事。首先,必须调用定义的函数。其次,从输入函数返回字符串,需要将其转换为整数,以便在代码使用它时使用它

还有一些其他的事情是很有用的。input函数是为打印提示而设计的,所以不必在输入()之后使用print,只需将输入与参数一起使用。然后,如果您先定义所有函数,然后让执行的代码跟随,则更容易阅读。您可以将直接执行的语句分散在整个代码中,但这会使您很难理解

def times_table(times,factor):
    """takes two positive integers and returns a list of lists

    int, int -> list of lists"""
    table = [[0]*(factor+1)]
    for i in range(1,times+1):
        table.append(list(range(0,i*factor+1,i)))
    return table  


def print_table(listx):
    """takes a list of lists of numbers and displays them on the screen

    list of numbers -> numbers"""
    for x in listx:
        for y in x:
            print('{0:<10}'.format(y), end='')
        print()

number1 = int(input("What is the maximum value for the first factor?"))
number2 = int(input("What is the maximun value for the second factor?"))
print("Here is your times table:")
table = times_table(number1, number2)
print_table(table)
def times_表(次数、系数):
“”接受两个正整数并返回列表列表
int,int->列表列表“”
表=[[0]*(系数+1)]
对于范围内的i(1,乘以+1):
表.追加(列表(范围(0,i*因子+1,i)))
返回表
def打印表格(列表X):
“”“获取数字列表并将其显示在屏幕上
数字列表->数字“”
对于listx中的x:
对于x中的y:

打印(“{0:这是否在脚本结尾?是的,在函数定义之后