求100内所有能被3整除但不能被5整除的正整数
要求1:每个数用“,”间隔
2:每10个数换一行
3:为while和for各写一个版本
如何换行呢
print(数字, end=',')连续输出
print()换行
count = 0
for i in range(1, 101):
if i % 3 == 0 and i % 5 != 0:
print(i, end=', ')
count += 1
if count % 10 == 0:
print()
count = 0
i = 1
while i <= 100:
if i % 3 == 0 and i % 5 != 0:
print(i, end=', ')
count += 1
if count % 10 == 0:
print()
i += 1
也可以先拼接字符串,再一次性输出
For版本:
result = ""
count = 0
for i in range(1, 101):
if i % 3 == 0 and i % 5 != 0:
result += str(i) + ", "
count += 1
if count % 10 == 0:
result += "\n"
print(result)
while版本:
result = ""
count = 0
i = 1
while i <= 100:
if i % 3 == 0 and i % 5 != 0:
result += str(i) + ", "
count += 1
if count % 10 == 0:
result += "\n"
i += 1
print(result)
str = []
result = ""
mystr = input("输入一个字符串:")
for s in mystr:
str.append(s)
last = str[-1]
str.remove(last)
str.insert(0, last)
for i in str:
result += i
print(result)
# 运行结果
'''
输入一个字符串:zxcvbnm
mzxcvbn
'''