我想检查一个数字是否是秒的倍数。以下代码有什么问题?
server.modules = (
"mod_rewrite",
"mod_access",
"mod_auth",
"mod_cgi",
"mod_accesslog" )
server.document-root = "/home/web_root/"
server.errorlog = "/home/lighttpd.error.log"
index-file.names = ( "index.php", "index.html",
"index.htm", "default.htm" )
server.event-handler = "poll" # needed on OS X
server.tag = "lighttpd/1.4.11 (Win32)"
accesslog.filename = "/home/access.log"
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
server.upload-dirs = ( "/var/tmp/lighttpd-upload/" )
错误:
def is_multiple(x,y):
if x!=0 & (y%x)==0 :
print("true")
else:
print("false")
end
print("A program in python")
x=input("enter a number :")
y=input("enter its multiple :")
is_multiple(x,y)
答案 0 :(得分:9)
您正在使用二元AND运算符 &
;你想要布尔AND运算符,and
:
x and (y % x) == 0
接下来,您希望将输入转换为整数:
x = int(input("enter a number :"))
y = int(input("enter its multiple :"))
你会在一行上获得NameError
表达式的end
,完全放弃,Python并不需要那些。
您可以测试只是 x
;在布尔上下文(例如if
语句)中,如果0:
if x and y % x == 0:
你的函数is_multiple()
应该只返回一个布尔值;将打印留给程序执行所有其他输入/输出的部分:
def is_multiple(x, y):
return x and (y % x) == 0
print("A program in python")
x = int(input("enter a number :"))
y = int(input("enter its multiple :"))
if is_multiple(x, y):
print("true")
else:
print("false")
如果使用条件表达式,最后一部分可以简化:
print("A program in python")
x = int(input("enter a number :"))
y = int(input("enter its multiple :"))
print("true" if is_multiple(x, y) else "false")
答案 1 :(得分:3)
有些事要提及:
function obj(name, lname){
this.name = name;
this.lastName = lname;
get fullName(){
return this.name + " " + this.lastName;
}
}
,而不是and
(二元运算符)&
) - 如果输入了数字以外的其他内容,您可能还想要捕获这应该有效:
int()
答案 2 :(得分:0)
使用and
运算符代替按位&
运算符。
您需要使用int()
def is_multiple(x,y):
if x!=0 and (y%x)==0 :
print("true")
else:
print("false")
print("A program in python")
x = int(input("enter a number :"))
y = int(input("enter its multiple :"))
is_multiple(x,y)