Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

python - print each 10 numbers in line always printing the first value alone

I have this loop which print each 10 numbers in line then move to next line

   for i in range(100, 201):
        if i % 10 == 0:
            print(i)

        else:
            print(i, end=" ", )

and this the result:

100
101 102 103 104 105 106 107 108 109 110
111 112 113 114 115 116 117 118 119 120
121 122 123 124 125 126 127 128 129 130
131 132 133 134 135 136 137 138 139 140
141 142 143 144 145 146 147 148 149 150
151 152 153 154 155 156 157 158 159 160
161 162 163 164 165 166 167 168 169 170
171 172 173 174 175 176 177 178 179 180
181 182 183 184 185 186 187 188 189 190
191 192 193 194 195 196 197 198 199 200

it printing first number in line alone, but the want the opposite, the last number alone, something like this

 100 101 102 103 104 105 106 107 108 109
 110 111 112 113 114 115 116 117 118 119
 120 121 122 123 124 125 126 127 128 129
 130 131 132 133 134 135 136 137 138 139
 140 141 142 143 144 145 146 147 148 149
 150 151 152 153 154 155 156 157 158 159
 160 161 162 163 164 165 166 167 168 169
 170 171 172 173 174 175 176 177 178 179
 180 181 182 183 184 185 186 187 188 189
 190 191 192 193 194 195 196 197 198 199
 200
question from:https://stackoverflow.com/questions/65867658/print-each-10-numbers-in-line-always-printing-the-first-value-alone

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have the correct code. The only thing is that you are breaking at mod 0. You should break at mod 9.

for i in range(100, 201):
        if i % 10 == 9:
            print(i)

        else:
            print(i, end=" ", )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...