使用已定义函数的脚本

时间:2015-04-26 23:56:25

标签: 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 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()

2 个答案:

答案 0 :(得分:1)

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

还有其他一些有用的事情。输入函数用于为您打印提示,因此不要使用print(后跟input()),只需使用带参数的输入。如果先定义所有函数,然后执行已执行的代码,则更容易阅读。你可以拥有直接分散在整个代码中的语句,但这很难让你理解。

useTexture

答案 1 :(得分:0)

假设number1number2将作为times_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)