如何编写以下模式

时间:2019-08-25 17:25:11

标签: python

我正在尝试在python中打印以下模式,但没有得到所需的输出:

0
2 2 
4 4 4 
6 6 6 6

我已经使用了以下代码,但未获得所需的输出

n = int (input("Enter number : "))
for i in range (n):
    print (i *2 )

4 个答案:

答案 0 :(得分:0)

尝试一下:

n = int (input("Enter number : "))
for i in range (n):
    print (' '.join([str(i * 2) for _ in range(i + 1)]) )

答案 1 :(得分:0)

您需要打印i+1次。

for i in range (n):
    for _ in range(i+1):
        print (i * 2," ", end='') # print without newlines
    print() # end the line

答案 2 :(得分:0)

遵循简单的代码可能会有所帮助:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="app.freeairdrop.io">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:appCategory="productivity"
        android:hardwareAccelerated="true"
        android:allowBackup="true"
        android:fullBackupContent="true"
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name=".ApplicationClass"
        tools:ignore="GoogleAppIndexingWarning">
        <activity
            android:name="app.freeairdrop.io.MainActivity"
            android:label="@string/app_name"
            android:configChanges="orientation|screenSize|screenLayout"
            android:resizeableActivity="false"
            android:supportsPictureInPicture="false"
            android:launchMode="singleInstance"
            android:alwaysRetainTaskState="true"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:scheme="https"
                    android:host="freeairdrop.io" />
            </intent-filter>


        </activity>
    </application>


    </manifest>

如果n = int (input("Enter number : ")) j = 1 k = 0 for i in range(n): s = str(k)+' ' print(s*j) j += 1 k += 2 ,则将打印以下内容:

n = 4

答案 3 :(得分:0)

n = int (input("Enter number : ")) 
count = 1
for i in range (n):
    res = str(i*2) + " "
    print (res * (i+1))
print n