打印从1到100的数字,跳过被3和5整除的数字

时间:2018-11-13 17:22:47

标签: python python-3.x algorithm if-statement while-loop

我想打印1-100的数字,并跳过被3和5整除的数字,而当我使用代码1时,我没有得到正确的输出,我会完全计数1-100

#CODE1
i=1
a=1
while i<=100:
    if (a%3==0 and a%5==0) :
           a=a+1
    else:
        print(a)
        a=a+1
    i=i+1

但是当我使用CODE-2时,我得到了预期的结果

#CODE2
i=1
a=1
while i<=100:
    if ((a%3 and a%5)==0) :
        a=a+1
    else:
        print(a)
        a=a+1
    i=i+1

注意代码的第四行,为什么第一代码有误?

12 个答案:

答案 0 :(得分:1)

考虑一下:

a = 10

(a%3 == 0) and (a%5 == 0)  # False
(a%3 and a%5) == 0         # True

第一次尝试错误地给了False,因为它需要同时满足两个条件。您需要使用or。如果您仔细查看,则会排除某些数字(例如15),这与同时包含35的数字相符。

第二次尝试是正确的,因为如果a不能被 3或5整除,则表达式的计算结果为False,而0 == False给出{{ 1}}。更惯用的是写:

True

答案 1 :(得分:1)

第一个程序不正确,因为您在6-7行中增加了计数器,而没有检查是否需要打印该数字。

更简洁的写法可能是:

for counter in xrange(1, 101):
    if not ((counter % 5 == 0) or (counter % 3 == 0)):
        print (a)

答案 2 :(得分:1)

问题是在CODE#1的第5行中将“和”更改为逻辑“或”。当前版本1仅在同时满足两个条件时才跳过数字。当满足一个或两个条件时,您想跳过数字。

PS:我想提出一种更快更有效的方法来获得此结果。

[![> Xcode\] Testing started on ' iPhone 6s Pink' \[Xcode\]  \[Xcode\] Testing
> failed: \[Xcode\]     Signing for "WebDriverAgentRunner" requires a
> development team. Select a development team in the Signing &
> Capabilities editor. \[Xcode\]    WebDriverAgentRunner: \[Xcode\]
>       WebDriverAgentRunner-Runner.app encountered an error (Failed to
> install or launch the test runner. (Underlying error: The file
> “WebDriverAgentRunner-Runner.app” couldn’t be opened because there is
> no such file. The file doesn’t exist. (Underlying error: The operation
> couldn’t be completed. No such file or directory))) \[Xcode\]  \[Xcode\]
> ** TEST EXECUTE FAILED ** \[Xcode\]  \[Xcode\]  \[XCUITest\] xcodebuild exited with code '65' and signal 'null' \[debug\] \[BaseDriver\] Event
> 'wdaStartFailed' logged at 1576751453184 (17:30:53 GMT+0700 (Indochina
> Time)) \[debug\] \[XCUITest\] Unable to launch WebDriverAgent because of
> xcodebuild failure: "xcodebuild failed with code 65 \[debug\] \[XCUITest\]
> xcodebuild error message: \[debug\] \[XCUITest\]  \[debug\] \[XCUITest\]
> 2019-12-19 17:30:53.133 xcodebuild\[13095:284322\] Error
> Domain=NSCocoaErrorDomain Code=260 "The file
> “WebDriverAgentRunner-Runner.app” couldn’t be opened because there is
> no such file."
> UserInfo={NSFilePath=/Users/keo.sidara/Library/Developer/Xcode/DerivedData/WebDriverAgent-brdadhpuduowllgivnnvuygpwhzy/Build/Products/Debug-iphoneos/WebDriverAgentRunner-Runner.app,
> NSUnderlyingError=0x7fa27214ee50 {Error Domain=NSPOSIXErrorDomain
> Code=2 "No such file or directory"}} \[debug\] \[XCUITest\] Test session
> results, code coverage, and logs: \[debug\] \[XCUITest\]
>   /Users/keo.sidara/Library/Developer/Xcode/DerivedData/WebDriverAgent-brdadhpuduowllgivnnvuygpwhzy/Logs/Test/Test-WebDriverAgentRunner-2019.12.19_17-30-52-+0700.xcresult
> \[debug\] \[XCUITest\] Testing started on ' iPhone 6s Pink' \[debug\]
> \[XCUITest\] Testing failed: \[debug\] \[XCUITest\]   Signing for
> "WebDriverAgentRunner" requires a development team. Select a
> development team in the Signing & Capabilities editor. \[debug\]
> \[XCUITest\]  WebDriverAgentRunner: \[debug\] \[XCUITest\]
>       WebDriverAgentRunner-Runner.app encountered an error (Failed to
> install or launch the test runner. (Underlying error: The file
> “WebDriverAgentRunner-Runner.app” couldn’t be opened because there is
> no such file. The file doesn’t exist. (Underlying error: The operation
> couldn’t be completed. No such file or directory))) \[debug\] \[XCUITest\]
> ** TEST EXECUTE FAILED **". Make sure you follow the tutorial at https://github.com/appium/appium-xcuitest-driver/blob/master/docs/real-device-config.md.
> Try to remove the WebDriverAgentRunner application from the device if
> it is installed and reboot the device. \[XCUITest\] Quitting and
> uninstalling WebDriverAgent \[XCUITest\] Shutting down sub-processes
> \[XCUITest\] Shutting down iproxy process (pid 13094) \[debug\] \[XCUITest\]
> iproxy exited with code 'null' \[debug\] \[XCUITest\] Removing WDA
> application from device \[XCUITest\] Error: Unable to launch
> WebDriverAgent because of xcodebuild failure: "xcodebuild failed with
> code 65 \[XCUITest\] xcodebuild error message: \[XCUITest\]  \[XCUITest\]
> 2019-12-19 17:30:53.133 xcodebuild\[13095:284322\] Error
> Domain=NSCocoaErrorDomain Code=260 "The file
> “WebDriverAgentRunner-Runner.app” couldn’t be opened because there is
> no such file."
> UserInfo={NSFilePath=/Users/keo.sidara/Library/Developer/Xcode/DerivedData/WebDriverAgent-brdadhpuduowllgivnnvuygpwhzy/Build/Products/Debug-iphoneos/WebDriverAgentRunner-Runner.app,
> NSUnderlyingError=0x7fa27214ee50 {Error Domain=NSPOSIXErrorDomain
> Code=2 "No such file or directory"}} \[XCUITest\] Test session results,
> code coverage, and logs: \[XCUITest\]
>   /Users/keo.sidara/Library/Developer/Xcode/DerivedData/WebDriverAgent-brdadhpuduowllgivnnvuygpwhzy/Logs/Test/Test-WebDriverAgentRunner-2019.12.19_17-30-52-+0700.xcresult
> \[XCUITest\] Testing started on ' iPhone 6s Pink' \[XCUITest\] Testing
> failed: \[XCUITest\]  Signing for "WebDriverAgentRunner" requires a
> development team. Select a development team in the Signing &
> Capabilities editor. \[XCUITest\]     WebDriverAgentRunner: \[XCUITest\]
>       WebDriverAgentRunner-Runner.app encountered an error (Failed to
> install or launch the test runner. (Underlying error: The file
> “WebDriverAgentRunner-Runner.app” couldn’t be opened because there is
> no such file. The file doesn’t exist. (Underlying error: The operation
> couldn’t be completed. No such file or directory))) \[XCUITest\] ** TEST
> EXECUTE FAILED **". Make sure you follow the tutorial at
> https://github.com/appium/appium-xcuitest-driver/blob/master/docs/real-device-config.md.
> Try to remove the WebDriverAgentRunner application from the device if
> it is installed and reboot the device. \[XCUITest\]     at
> quitAndUninstall
> (/usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/lib/driver.js:488:13)
> \[XCUITest\]     at
> /usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/lib/driver.js:518:9
> \[debug\] \[XCUITest\] Not clearing log files. Use `clearSystemFiles`
> capability to turn on. \[debug\] \[iOSLog\] Stopping iOS log capture
> \[debug\] \[BaseDriver\] Event 'newSessionStarted' logged at 1576751453341
> (17:30:53 GMT+0700 (Indochina Time)) \[debug\] \[W3C\] Encountered
> internal error running command: Error: Unable to launch WebDriverAgent
> because of xcodebuild failure: "xcodebuild failed with code 65 \[debug\]
> \[W3C\] xcodebuild error message: \[debug\] \[W3C\]  \[debug\] \[W3C\]
> 2019-12-19 17:30:53.133 xcodebuild\[13095:284322\] Error
> Domain=NSCocoaErrorDomain Code=260 "The file
> “WebDriverAgentRunner-Runner.app” couldn’t be opened because there is
> no such file."
> UserInfo={NSFilePath=/Users/keo.sidara/Library/Developer/Xcode/DerivedData/WebDriverAgent-brdadhpuduowllgivnnvuygpwhzy/Build/Products/Debug-iphoneos/WebDriverAgentRunner-Runner.app,
> NSUnderlyingError=0x7fa27214ee50 {Error Domain=NSPOSIXErrorDomain
> Code=2 "No such file or directory"}} \[debug\] \[W3C\] Test session
> results, code coverage, and logs: \[debug\] \[W3C\]
>   /Users/keo.sidara/Library/Developer/Xcode/DerivedData/WebDriverAgent-brdadhpuduowllgivnnvuygpwhzy/Logs/Test/Test-WebDriverAgentRunner-2019.12.19_17-30-52-+0700.xcresult
> \[debug\] \[W3C\] Testing started on ' iPhone 6s Pink' \[debug\] \[W3C\]
> Testing failed: \[debug\] \[W3C\]     Signing for "WebDriverAgentRunner"
> requires a development team. Select a development team in the Signing
> & Capabilities editor. \[debug\] \[W3C\]  WebDriverAgentRunner: \[debug\]
> \[W3C\]       WebDriverAgentRunner-Runner.app encountered an error (Failed
> to install or launch the test runner. (Underlying error: The file
> “WebDriverAgentRunner-Runner.app” couldn’t be opened because there is
> no such file. The file doesn’t exist. (Underlying error: The operation
> couldn’t be completed. No such file or directory))) \[debug\] \[W3C\] **
> TEST EXECUTE FAILED **". Make sure you follow the tutorial at
> https://github.com/appium/appium-xcuitest-driver/blob/master/docs/real-device-config.md.
> Try to remove the WebDriverAgentRunner application from the device if
> it is installed and reboot the device. \[debug\] \[W3C\]     at
> quitAndUninstall
> (/usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/lib/driver.js:488:13)
> \[debug\] \[W3C\]     at
> /usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/lib/driver.js:518:9
> \[HTTP\] <-- POST /wd/hub/session 500 18368 ms - 3779][1]][1]

答案是:

import numpy as np
numbers = np.arange(1,101,1)
print('Original numbers \n', numbers)
print('Required numbers \n', numbers[(numbers%3!=0) & (numbers%5!=0)])

答案 3 :(得分:1)

检查这也有效! 100%

def result(N):
           for num in range(N):
          if num % 3 == 0 and num % 5 == 0:
             print(str(num) + " ", end="")
          else:
             pass
    if __name__ == "__main__":
       N = 100
    
       result(N)

答案 4 :(得分:0)

一个更清晰的答案

$("form#data").submit(function(e) {
    e.preventDefault();    
    var formData = new FormData(this);

$.ajax({
    url: window.location.pathname,
    type: 'POST',
    data: formData,
    success: function (data) {
        alert(data)
    },
    cache: false,
    contentType: false,
    processData: false
});
});

答案 5 :(得分:0)

n=1

while 1<=n<=100:

    if((n%3 and n%5)==0) :
        False

    else:
        print(n)      
    n=n+1

答案 6 :(得分:0)

在第一个代码中,您使用的几乎与您所需要的相反,即语句。我建议您将换成语句。

#CODE
i=1
a=1
while i<=100:
    if (a%3==0 or a%5==0) :
           a=a+1
    else:
        print(a)
        a=a+1
    i=i+1

在第二个代码中,您使用的 nand 在这种情况下与 or 非常相似,因此适用于数据集的第一部分。但是,在更复杂的代码中,这会使您对某些检查产生误判,因此,如果没有严格的要求,我应该避免这样做。

答案 7 :(得分:0)

我从你的问题中得到了这个输出。

for i in range(1, 100):
      if(i%3==0 or i%5==0):
         continue
      print(i)

答案 8 :(得分:0)

这样做。

for i in range(1, 101):
    if i % 3 != 0 and i % 5 != 0:
        print(i)

答案 9 :(得分:0)

for i in range(1, 101):

   if i%3==0 and i%5==0:

    continue

   print(i)

答案 10 :(得分:0)

试试这个:

r = range(1,101,1)
for i in r:
      if i%3!=0 and i%5!=0:
            print(i)

答案 11 :(得分:0)

i=1

while(i<=100):
if(i%3!=0 and i%5!=0):
    print(i)
i=i+1

试试这个。简单干净,容易