初学者Python帮助 - 主要功能

时间:2017-02-02 03:32:51

标签: python main

我在使用此代码时遇到了一些问题:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_comment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.erik.appname.CommentActivity">


<android.support.v7.widget.RecyclerView
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="470dp"
    android:id="@+id/posted_comments">

</android.support.v7.widget.RecyclerView>



<LinearLayout
    android:id="@+id/write_comment"
    android:gravity="bottom"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/posted_comments"
    android:layout_alignParentStart="true">

    <EditText
        android:layout_alignParentBottom="true"
        android:paddingLeft="10dp"
        android:layout_width="0dp"
        android:layout_weight="8"
        android:background="@drawable/input_outline2"
        android:layout_height="match_parent"
        android:inputType="textPersonName|textMultiLine"
        android:text=""
        android:gravity="left|center"
        android:hint="Comment..."
        android:textSize="20dp"
        android:maxLength="100"
        android:id="@+id/current_comment" />

    <ImageButton
        android:src="@drawable/submit"
        android:scaleType="fitCenter"
        android:padding="5dp"
        android:background="@drawable/input_outline2"
        android:layout_alignParentBottom="true"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:id="@+id/submit"
        android:layout_toEndOf="@+id/current_comment"
        />


</LinearLayout>

我要做的是使用main函数多次测试sumList。上面,数字加在一起产生18.我想要合并主函数。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

好的,所以我不能100%肯定你说的是什么,但是如果你只是说我如何用main函数测试sumList那么你可以做如下的事情:

testCases = [([1,2,3],6) , ([1,1,1],3), ([10,10,12], 32)]
def main():
    for testCase, answer in testCases:
        if sumList(testCase) != answer:
            print("False")
    print("Everything checks out")

# then to actually call your main function like something similar to cpp
if __name__ == '__main__':
    main()

这个约定if __name__ == '__main__':通常是你如何在python中访问你的主要调用。

如果您对主要功能的观点感到好奇,那么就有一个非常好的Stack链接here。再次堆叠救援。大量使用它!

答案 1 :(得分:0)

这样的事情怎么样?

def sumList(nums):
    sum = 0
    for num in nums:
        sum = sum + num
    return sum

def main():
    print("Test One:")
    print(sumList([5,2,4,7]))
    print("Test Two:")
    print(sumList([1,2,3,4]))
    print("Test Three:")
    print(sumList([0.5, 0.5, 0.5, 0.5]))

这应该产生这样的输出

Test One:
18
Test Two:
10
Test Three:
2.0