小编Mei*_*oob的帖子

Python for循环结束,不满足任何条件

我有一个程序要求用户输入两个数字,并检查数字之间的每个数字是否都可以用5和7进行除法。如果范围内没有数字,我希望程序输出类似“找不到数字”的内容除以5和7。例如,如果用户输入3和4,它将显示“找不到数字”

我尝试了几种不同的方法,但是都没有用。

start = int(input("Start: "))
stop = int(input("Stop: "))


for number in range(start, (stop+1),1):
    if number % 5 == 0 and number % 7 ==0:
        print("Number", number, "can be divided with 5 and 7")
        print("Stop search")
        break
    elif  number % 5 == 0 and number % 7 !=0:
        print(number, "can't be divided with 7, next.")
    elif number % 5 != 0:
        print(number,"can't be divided with 5, next.")
Run Code Online (Sandbox Code Playgroud)

python python-3.x

3
推荐指数
1
解决办法
71
查看次数

向后打印字符串,同时删除字符

这会在从开头删除字符的同时打印单词。

word = "word"

length = len(word)
for s in range(0, length):
    print(word[s:])
    s=+1
Run Code Online (Sandbox Code Playgroud)

所以输出是

word
ord
rd
d
Run Code Online (Sandbox Code Playgroud)

如何翻转它,以便在删除字符时向后打印单词?
这样输出将是:

drow
row
ow
w
Run Code Online (Sandbox Code Playgroud)

python python-3.x

2
推荐指数
1
解决办法
60
查看次数

要列出的 Python 字符计数器

我有一个包含字符的列表

chars  =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
           'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '-']
Run Code Online (Sandbox Code Playgroud)

然后是一个 Counter 对象

from collections import Counter

s = 'testing'
counter = Counter(s)
print(counter)

 >>> Counter({'t': 2, 'e': 1, 's': 1, 'i': 1, 'n': 1, 'g': 1})
Run Code Online (Sandbox Code Playgroud)

如何从此Counter对象获取与列表具有相同顺序和长度的chars列表?输出列表将包含整数,因此如果字符串不包含字符,则会有一个0.

python counter

1
推荐指数
1
解决办法
78
查看次数

标签 统计

python ×3

python-3.x ×2

counter ×1