2017-09-05 61 views
0

我想學python &而試圖解決這個測驗在網站上,我得到一個奇怪的錯誤,這是斷言失敗的第二個參數,而如果elif說法正確的是&應給予正確的輸出(但它給人一種錯誤的意外輸出)如果ELIF語句與&是給出意想不到的輸出

的錯誤是:

"C:\Program Files\Python36\python.exe" C:/Users/.../PycharmProjects/pythonSnakegame/snakeGame.py 
Fizz Buzz 
Traceback (most recent call last): 
Fizz Buzz 
    File "C:/Users/..../PycharmProjects/pythonSnakegame/snakeGame.py", line 28, in <module> 
    assert checkio(6) == "Fizz", "6 is divisible by 3" 
AssertionError: 6 is divisible by 3 

Process finished with exit code 1 

def checkio(number): 
    # Your code here 
    # It's main function. Don't remove this function 
    # It's using for auto-testing and must return a result for check. 
    if number % 3 == 0 & number % 5 == 0: 
     result = "Fizz Buzz" 
    elif number % 3 == 0: 
     result = "Fizz" 
    elif number % 5 == 0: 
     result = "Buzz" 

    # replace this for solution 
    print (result) 
    return result 
    # return str(number) 


# Some hints: 
# Convert a number in the string with str(n) 

# These "asserts" using only for self-checking and not necessary for auto-testing 
if __name__ == '__main__': 
    assert checkio(15) == "Fizz Buzz", "15 is divisible by 3 and 5" 
    assert checkio(6) == "Fizz", "6 is divisible by 3" 
    assert checkio(5) == "Buzz", "5 is divisible by 5" 
    assert checkio(7) == "7", "7 is not divisible by 3 or 5" 
    print("Coding complete? Click 'Check' to review your tests and earn cool rewards!") 

感謝您的幫助

+4

布爾和運算符是'和',而不是'&'。 –

+0

https://stackoverflow.com/questions/3845018/boolean-operators-vs-bitwise-operators –

+1

爲了記錄,聲明如下所示:'(number%3)==(0&(number%5) )== 0'。所以5檢查的可分性完全由'0&'在那裏去除。 –

回答

5

& operato r代表二進制操作,您應該使用and布爾型運算符)代替。

我一直想知道爲什麼它不工作

&被禁止用於5模塊操作檢查,所以基本上都被3整除那裏得到True,檢查此輸出測試:

number = 12 
number % 3 == 0 & number % 5 == 0 
True 
+0

非常感謝,它的工作! 我一直在想,爲什麼它不工作,謝謝 –

+0

感謝您的插圖。 –

相關問題