【问题标题】:If count == multiple of 5 [duplicate]如果计数 == 5 的倍数 [重复]
【发布时间】:2021-04-08 11:44:45
【问题描述】:

我有一个不和谐的垃圾邮件机器人。

import pyautogui

from time  import sleep 

time = 0 
while time != 10:
        time += 1
        sleep(1)
        print ("Get Ready" + str (time) )

def spam(msg, maxMsg):
        count = 0
        while count != maxMsg:
                count += 1
                print("send message: " + str(count ))
                pyautogui.write(msg)
                pyautogui.press("enter")
                if count == 5 or count == 10 or count == 15:
                        sleep(8)

 spam('Test', 15)

我的问题在于if count,这里的上限是 15,但我希望它是 500,而不写 or count == 20 or count == 25,直到 500 有没有办法说在每个 5 的倍数上睡眠?

【问题讨论】:

  • 使用取模运算符。
  • 当count除以5时,你可以检查余数是否为0。if count % 5 == 0:
  • if count % 5 == 0
  • 你在学走路之前就在跑步。几乎您将遇到的每种编程语言都会有某种模数运算。

标签: python spam


【解决方案1】:
import pyautogui

from time  import sleep 

time = 0 
while time != 10:
        time += 1
        sleep(1)
        print ("Get Ready" + str (time) )

def spam(msg, maxMsg):
        count = 0
        while count != maxMsg:
                count += 1
                print("send message: " + str(count ))
                pyautogui.write(msg)
                pyautogui.press("enter")
                if count % 5 == 0 and count <= 500
                        sleep(8)

 spam('Test', 15)

使用模运算符:它返回两个数相除的余数。 也就是说:如果 A % B = C (A, B, C 是整数) 那么有一个整数 K 使得 A = B * K + C。在你的具体问题中,如果 count % 5 = 0,那么 count 是一个5 的倍数,因为有一个整数 K 使得 A = 5 * K + 0 = 5 * K。这表明 A 必须是 5 的倍数。

我忘了:当然,您需要检查 count 是否小于或等于 500。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    相关资源
    最近更新 更多