如何確定一個整數是2的倍數而不是3的倍數'是僅爲2的倍數'?使用python。如何確定整數是2的倍數還是3的倍數print'<myint >只是2的倍數。'?使用python
if myint%2:
print(str(myint), "is a multiple of 2 only")
我如何得到它的輸出「而不是3的倍數」這樣做
your_number%2 == 0 and your_number%3 != 0
凡%
如何確定一個整數是2的倍數而不是3的倍數'是僅爲2的倍數'?使用python。如何確定整數是2的倍數還是3的倍數print'<myint >只是2的倍數。'?使用python
if myint%2:
print(str(myint), "is a multiple of 2 only")
我如何得到它的輸出「而不是3的倍數」這樣做
your_number%2 == 0 and your_number%3 != 0
凡%
什麼是模運算符和右手分左手操作操作數並返回餘數。所以如果餘數等於0
,則左操作數是右操作數的倍數。
因此
your_number%2 == 0
回報True
如果your_number
是2
和
your_number%3 != 0
回報倍數True
如果your_number
不是3
倍數爲您提供一個完整的回答,你需要的是:
if myint%2 == 0 and myintr%3 != 0:
print(str(myint), "is a multiple of 2 and not a multiple of 3")
這是你如何做到這一點:
#first checks if myint/2 doesn't has a reminder
#then checks if myint/3 has a reminder
if not myint % 2 and myint % 3:
print(myint,"is a multiple of 2 only")
,或者如果你想:
if myint % 2 == 0 and myint % 3 != 0:
print(myint,"is a multiple of 2 only")
都工作在同一
這是FizzBuzz。它非常受歡迎。請Google它。 – TigerhawkT3